我有一个 mainViewController 和一个 UIPickerView。现在一切都设置好了,但是当我运行应用程序并选择 textField 时,我的 UIPickerView 列表中出现了问号。
现在我才知道,那是因为我没有为它连接委托(delegate)。 但是,如果我为其连接委托(delegate),则不会填充 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。
我错过了什么??
干杯
最佳答案
您需要实现选择器委托(delegate)方法“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];
}
我认为这可能有用。
关于objective-c - Objective-C - 设置 UIPickerView 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201171/