Question

I have three different UIPickerViews in one view/viewcontroller. Instead of making the viewController the delegate and datasource for all three UIPickerViews, I created separate subclass files for each of the UIPickerViews and imported those files into the viewController. I created outlets (click and dragging from storyboard) for each of the UIPickerViews in the ViewController. Therefore, in viewDidLoad of the viewController, I connected the outlet to the datasource/delegates. Here is one example with the outlet for self.timePicker

self.timePicker.delegate = [self timePickerDataDelegate];
self.timePicker.dataSource = [self timePickerDataDelegate];

where [self timePickerDataDelegate] calls the constructor

-(TimePicker *)timePickerDataDelegate
{

    if (!_timePickerDataDelegate) _timePickerDataDelegate = [[TimePicker alloc] init];
    return _timePickerDataDelegate;
}

In the timePicker class, I implemented the required methods for it to serve as the delegate and datasource. Indeed, when I run the app all three of the UIPickerViews are populated with the correct data. However, I'm having a problem getting the data from the picker view. Since my UIPickerViews are in a modal view, I try to get the data in prepareForSegue, which gets called when the modal is dismissed. However, when I do this to get the time value selected I get the error

  No visible interface for UIPickerView declares the selector pickerView:titleForRow:forComponent

 NSString *time = [self.timePicker pickerView:self.timePicker titleForRow:[self.timePicker selectedRowInComponent:0] forComponent:0];

Can you explain what I'm doing wrong?

Was it helpful?

Solution

The method pickerView:titleForRow:forComponent: is a method of the UIPickerViewDelegate, not the UIPickerView.

The line:

NSString *time = [self.timePicker pickerView:self.timePicker titleForRow:[self.timePicker selectedRowInComponent:0] forComponent:0];

should be:

NSString *time = [self.timePickerDataDelegate pickerView:self.timePicker titleForRow:[self.timePicker selectedRowInComponent:0] forComponent:0];

or:

NSString *time = [self.timePicker.delegate pickerView:self.timePicker titleForRow:[self.timePicker selectedRowInComponent:0] forComponent:0];

OTHER TIPS

The problem was that the first element in the method should actually be the delegate

self.timePickerDataDelegate

and the other elements the outlets

self.timePicker

Here it is in full

   NSString *time = [self.timePickerDataDelegate pickerView:self.timePicker titleForRow:`[self.timePicker selectedRowInComponent:0] forComponent:0];`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top