简体   繁体   中英

UIPickerView delegate methods not being called even though delegate has been set after [[UIPickerView alloc] init]

Title says it all really I have a method that is called to create the UIPickerView and in the same view controller as the delegate methods. The view controller implements the UIPickerViewDataSource and UIPickerViewDelegate protocols

// ViewController.m

- (void)setUpPickerView{
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator=YES;
    self.groupsTextField.inputView = pickerView;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.pickerViewData count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.pickerViewData[row];
}

I have seen lots of answers where people say that if pickerView:numberOfRowsInComponent:component returns 0 this would stop the other delegate methods from been called but this is not the case for me when I set breakpoints none of the delegate methods are called at all.

Thanks in advance for the help!

Thanks everyone for your help in the end I did what I thought I would have to and just deleted the text field and everything associated with it in the view controller and started again. I see no real difference in my new code to what I posted so I would assume it was something to do with the text field in IB.

将Picker委托添加到.h文件

  @ interface ViewController :UIViewController< UIPickerViewDataSource , UIPickerViewDelegate >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM