我有UIPickerView
我正在尝试设置数据源;设置datasource
后,我将其放入模式弹出框中进行显示。这是代码 - manicuristArray定义为NSArray
,pvManicurist是UIPickerView
,UIPickerView
的所有代表都已正确设置,根据我在SO上找到的样本:< / p>
-(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”
我认为这会导致UIPicker
视图不被放入UIPopover
。我还有其他几个popovers,其中都有UIDatePickers
,并且它们运行正常。我看过SO和Google,发现没有回答这个特殊问题,这就是:为什么这不起作用?以及如何修复构建错误?
更新:以下是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 > {
答案 0 :(得分:4)
您无法将数组指定为数据选择器的数据源,因为数组不提供选择器所需的信息。为了正常工作,选择器至少需要回答这三个问题:
数据源通过实施数据源协议来回答前两个问题:您需要
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
返回组件数量,
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
返回给定组件中的行数。在您的类中实现这两种方法,然后分配
pvManicurist.dataSource = self;
当然你也需要实现委托的方法,但是因为你分配了popoverController.delegate = (id)self;
,你已经有机会了。
答案 1 :(得分:0)
问题在于作业
pvManicurist.dataSource = self.manicuristArray;
dataSource
必须是符合协议UIPickerViewDataSource
的对象,即id<UIPickerViewDataSource
类型的对象。显然NSArray
不符合协议。
您需要分配符合该协议的控制器,而不是数据本身。
答案 2 :(得分:0)
您收到该错误消息是因为您尝试将NSArray*
设置为选择器视图的数据源。选择器视图的数据源应该是符合UIPickerViewDataSource
协议的类的实例。
有关此协议的更多信息,请访问iOS文档的UIPickerViewDataSource Protocol Reference。