简体   繁体   English

目标C-设置UIPickerView委托

[英]Objective C - Setting UIPickerView Delegate

I have one mainViewController and I have a UIPickerView. 我有一个mainViewController和一个UIPickerView。 Now all is set up well, but when I run the app and select the textField I get Question Marks in my UIPickerView list. 现在一切设置都很好,但是当我运行应用程序并选择textField时,我的UIPickerView列表中会出现问号。

Now I have just learned that, that is because I have not connected the delegate for it. 现在我才知道,这是因为我没有为此连接代表。 However if I connect the delegate for it, then the textField does not get populated. 但是,如果我为其连接委托,则不会填充textField。

The TextField is in mainViewController. TextField位于mainViewController中。

So in my MainViewController.h I have imported the UIPickerView and have this code - which is all without errors. 因此,在MainViewController.h中,我已导入UIPickerView并具有以下代码-全部都没有错误。 _pv is the UIPickerView. _pv是UIPickerView。

 _pv = [[CategoryPicker alloc] init];

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

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

Now as you can see the Delegate is set to self, this will allow for the textField to be populated. 现在您可以看到Delegate设置为self,这将允许填充textField。

If I set that to _pv the then I get the list displayed properly, but the textField is not populated. 如果将其设置为_pv,则可以正确显示列表,但不会填充textField。

What am I missing?? 我想念什么?

Cheers 干杯

You need to implement the picker delegate method "pickerView:didSelectRow:inComponent:" and you need to set the selected text as textfild text. 您需要实现选择器委托方法“ pickerView:didSelectRow:inComponent:”,并且需要将所选文本设置为textfild文本。 see the following code for reference, 请参阅以下代码以供参考,

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

I think this may useful. 我认为这可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM