我有一个要设置数据源的UIPickerView;一旦设置了datasource,我就把它放置到一个模式弹出中来显示。下面是代码- manicuristArray被定义为NSArray,pvManicurist是UIPickerView,UIPickerView的所有委托都已正确设置,根据我在SO上找到的示例):
-(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:的委托方法
//-- 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文件的接口:
@interface AppointmentsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource > {发布于 2013-04-06 21:20:29
不能将数组指定为数据选择器的数据源,因为数组不提供选择器需要的信息。为了正确工作,选择者至少需要回答以下三个问题:
数据源通过实现数据源协议来回答前两个问题:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView若要返回组件数,请执行以下操作
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component若要返回给定组件中的行数,请执行以下操作。在类中实现这两个方法,然后分配
pvManicurist.dataSource = self;当然,您也需要实现委托的方法,但是由于您分配了popoverController.delegate = (id)self;,所以您已经这样做了。
发布于 2013-04-06 21:20:55
问题是分配
pvManicurist.dataSource = self.manicuristArray;dataSource必须是符合协议UIPickerViewDataSource的对象,即id<UIPickerViewDataSource类型的对象。显然,NSArray不符合协议。
您需要指定一个符合该协议的控制器,而不是数据本身。
发布于 2013-04-06 21:24:09
您收到该错误消息是因为您试图将NSArray*设置为选择器视图的数据源。选择器视图的数据源应该是符合UIPickerViewDataSource协议的类的实例。
有关此协议的更多信息,请访问UIPickerViewDataSource协议参考 of iOS文档。
https://stackoverflow.com/questions/15856036
复制相似问题