可以填充选择器视图但不能从中获取数据

时间:2014-04-27 16:47:49

标签: ios objective-c uipickerview

我在一个视图/ viewcontroller中有三个不同的UIPickerView。我没有让viewController成为所有三个UIPickerViews的委托和数据源,而是为每个UIPickerView创建了单独的子类文件,并将这些文件导入到viewController中。我为ViewController中的每个UIPickerView创建了插件(从故事板中单击并拖动)。因此,在viewController的viewDidLoad中,我将插座连接到datasource / delegates。以下是self.timePicker出口的一个示例

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

其中[self timePickerDataDelegate]调用构造函数

-(TimePicker *)timePickerDataDelegate
{

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

在timePicker类中,我实现了它所需的方法作为委托和数据源。实际上,当我运行应用程序时,所有三个UIPickerView都填充了正确的数据。但是,我从选择器视图中获取数据时遇到问题。由于我的UIPickerViews处于模态视图中,因此我尝试获取prepareForSegue中的数据,该模式在模式被解除时被调用。但是,当我这样做以获得时间值时,我得到错误

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

你能解释一下我做错了吗?

2 个答案:

答案 0 :(得分:1)

方法pickerView:titleForRow:forComponent:UIPickerViewDelegate的方法,而不是UIPickerView

该行:

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

应该是:

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

或:

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

答案 1 :(得分:0)

问题是方法中的第一个元素实际上应该是委托

self.timePickerDataDelegate

和其他元素出口

self.timePicker

这里是完整的

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