首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成错误:将不兼容类型“id<UIPickerViewDataSource>”分配给“NSArray*”

生成错误:将不兼容类型“id<UIPickerViewDataSource>”分配给“NSArray*”
EN

Stack Overflow用户
提问于 2013-04-06 21:07:31
回答 3查看 1.6K关注 0票数 3

我有一个要设置数据源的UIPickerView;一旦设置了datasource,我就把它放置到一个模式弹出中来显示。下面是代码- manicuristArray被定义为NSArray,pvManicurist是UIPickerViewUIPickerView的所有委托都已正确设置,根据我在SO上找到的示例):

代码语言:javascript
复制
    -(void) showModalManicurist:(int)tag {

    UIViewController* popoverContent = [[UIViewController alloc] init];

    UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 216)];
    popoverView.backgroundColor = [UIColor redColor];
    popoverContent.contentSizeForViewInPopover = CGSizeMake(300.0, 216.0);

    //  define the UIPickerView
    pvManicurist.frame = CGRectMake(0, 0, 300, 216);

    //  fill the names from the d/b into manicuristArray
    PreferenceData *pv = [PreferenceData MR_findFirst];  //  (everything is in one record)
    NSLog(@"pv.aStaffPos1: %@", pv.aStaffPos1);

    if(pv)  {   //  fill the UIPickerView
        self.manicuristArray = [[NSArray alloc] initWithObjects: pv.aStaffPos1, pv.aStaffPos2, pv.aStaffPos3, pv.aStaffPos4, pv.aStaffPos5,
                           pv.aStaffPos6, nil];
        NSLog(@"\nmanicuristArray.count: %d",manicuristArray.count);

        pvManicurist.dataSource = self.manicuristArray;        [pvManicurist reloadAllComponents];

     }

    //  add it to the popover
    [popoverView addSubview:pvManicurist];
    popoverContent.view = popoverView;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate = (id)self;
    [popoverController setPopoverContentSize:CGSizeMake(300, 216) animated:NO];

    //  show it below the staff name textbox
    [popoverController presentPopoverFromRect:boStaff.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp
                                     animated:YES];
}

问题是我收到了这个构建警告:

生成错误:从不兼容类型'NSArray *‘将'id’赋值给‘id’

我认为这会导致UIPicker视图不被放入UIPopover中。我还有几个其他的弹出式,都有UIDatePickers,它们工作得很好。我已经看过了,谷歌也没有找到任何答案来回答这个特殊的问题,那就是:为什么这不起作用?以及如何修复构建错误?

更新:下面是UIPickerView:的委托方法

代码语言:javascript
复制
//--  sets number of columns
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pvManicurist  {

    return 1; // One column
}

//--  sets count of manicuristArray
-(NSInteger)pickerView:(UIPickerView *)pvManicurist numberOfRowsInComponent:(NSInteger)component  {

    return manicuristArray.count;  //set number of rows
}

//--  sets the component with the values of the manicuristArray
-(NSString *)pickerView:(UIPickerView *)pvManicurist titleForRow:(NSInteger)row forComponent:(NSInteger)component  {

    return [manicuristArray objectAtIndex:row];  //set item per row
}

下面是来自.h文件的接口:

代码语言:javascript
复制
@interface AppointmentsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource  >  {
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-06 21:20:29

不能将数组指定为数据选择器的数据源,因为数组不提供选择器需要的信息。为了正确工作,选择者至少需要回答以下三个问题:

  • 一个采摘者应该有多少个部件,
  • 每个组件有多少行,以及
  • 在每个组件的每一行中放什么数据。

数据源通过实现数据源协议来回答前两个问题:

代码语言:javascript
复制
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

若要返回组件数,请执行以下操作

代码语言:javascript
复制
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

若要返回给定组件中的行数,请执行以下操作。在类中实现这两个方法,然后分配

代码语言:javascript
复制
pvManicurist.dataSource = self;

当然,您也需要实现委托的方法,但是由于您分配了popoverController.delegate = (id)self;,所以您已经这样做了。

票数 4
EN

Stack Overflow用户

发布于 2013-04-06 21:20:55

问题是分配

代码语言:javascript
复制
pvManicurist.dataSource = self.manicuristArray;

dataSource必须是符合协议UIPickerViewDataSource的对象,即id<UIPickerViewDataSource类型的对象。显然,NSArray不符合协议。

您需要指定一个符合该协议的控制器,而不是数据本身。

票数 0
EN

Stack Overflow用户

发布于 2013-04-06 21:24:09

您收到该错误消息是因为您试图将NSArray*设置为选择器视图的数据源。选择器视图的数据源应该是符合UIPickerViewDataSource协议的类的实例。

有关此协议的更多信息,请访问UIPickerViewDataSource协议参考 of iOS文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15856036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档