문제

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?

도움이 되었습니까?

해결책

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];

다른 팁

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];`
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top