【问题标题】:Objective C - Setting UIPickerView DelegateObjective C - 设置 UIPickerView 委托
【发布时间】:2012-06-27 09:46:20
【问题描述】:

我有一个 mainViewController 和一个 UIPickerView。现在一切都设置好了,但是当我运行应用程序并选择 textField 时,我的 UIPickerView 列表中出现了问号。

现在我才知道,那是因为我没有为它连接代理。 但是,如果我为它连接代理,则不会填充 textField。

TextField 在 mainViewController 中。

所以在我的 MainViewController.h 中,我已经导入了 UIPickerView 并拥有这段代码 - 这一切都没有错误。 _pv 是 UIPickerView。

 _pv = [[CategoryPicker alloc] init];

[_pv.categoryPicker setDelegate:self];
[_pv.categoryPicker setDataSource:_pv];

[_appPriorityTxtFld setInputView:_pv];
[_appPriorityTxtFld setInputAccessoryView:toolbar];

现在您可以看到 Delegate 设置为 self,这将允许填充 textField。

如果我将其设置为 _pv,那么我会正确显示列表,但不会填充 textField。

我错过了什么??

干杯

【问题讨论】:

  • 你实现pickerView:didSelectRow:inComponent:了吗?您应该在该方法中更新文本字段的文本。
  • 在 UIPickerView 中?所以我需要反过来导入它?
  • 但是我已经实现了 - 这就是我在 textField 中得到结果的原因。

标签: objective-c ios delegates datasource uipickerview


【解决方案1】:

你需要实现picker委托方法“pickerView:didSelectRow:inComponent:”并且你需要将选中的文本设置为textfild文本。参考以下代码,

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [dataArray objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    textField.text = [dataArray objectAtIndex:row];
}

我认为这可能有用。

【讨论】:

  • 是的,这或多或少起到了作用。它使选择器正常运行,但我认为我更希望将其子类化。
  • 你可以为它做一件事。也就是说,在自定义类中实现选取器的所有委托和数据源方法,并将选定的字符串传递给文本字段的文本。因此,您无需设置不同的委托和数据源,每件事都将在自定义类本身中。唯一的事情是,您需要在选择器中选择行时访问文本字段对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多