我有一个 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];
}

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



我相信这会导致 UIPicker View 不被放入 UIPopover 。我还有其他几个弹出框,都带有 UIDatePickers,它们工作正常。我查看了 SO 和 Google 并没有发现任何可以回答这个特定问题的内容,即:为什么这不起作用?以及如何修复构建错误?

更新:这里是 UIPickerView 的委托(delegate)方法:
//--  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):
@interface AppointmentsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource  >  {

最佳答案

您不能将数组分配为数据选取器的数据源,因为数组不提供选取器所需的信息。为了正常工作,选择器至少需要回答以下三个问题:

  • 一个picker应该有多少个组件,
  • 每个组件有多少行,
  • 每个组件的每一行要放什么数据。

  • 数据源通过实现数据源协议(protocol)来回答前两个问题:你需要
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    

    返回组件的数量,以及
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    

    返回给定组件中的行数。在您的类中实现这两种方法,然后分配
    pvManicurist.dataSource = self;
    

    当然,您还需要实现委托(delegate)的方法,但是由于您分配了 popoverController.delegate = (id)self; 很可能您已经完成了。

    关于ios - 构建错误 : Assigning to 'id<UIPickerViewDataSource>' from incompatible type 'NSArray *' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15856036/

    10-13 23:59