Search code examples
iosobjective-cuipickerviewuipickerviewdatasourceuipickerviewdelegate

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!


Solution

  • 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.