UIPickerView - 第一行选择不会调用didSelectRow [英] UIPickerView - 1st row selection does not call didSelectRow

查看:628
本文介绍了UIPickerView - 第一行选择不会调用didSelectRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UIView 上有 UIPickerView 以及 UITextField 。我让用户从选择器中选择一个值或在文本字段中输入自定义值。

I have a UIPickerView on my UIView along with a UITextField. I am letting the user select a value from the picker or enter a custom value in the text field.

当用户从选择器中选择任何选项时,变量值为改变了(在 pickerView:didSelectRow:inComponent:)方法。但是如果用户没有在选择器上选择任何值(因为他们想要选择已经选择的第一个选项),则不会调用此方法,并且选择不会反映在变量中。

When the user selects any option from the picker, the variable values are changed fine (in the pickerView:didSelectRow:inComponent:) method. But if the user does not select any value on the picker (coz they want to select the 1st option which is already selected), this method is not called and the selection is not reflected in the variable.

我尝试将变量的默认值设置为选择器中的第一个选项。但在这种情况下,当用户编辑文本字段时,变量将具有文本字段的文本。现在,如果用户决定再次选择第一个值,则新值不会反映在变量中。

I tried setting the default value of the variable to the first option in the picker. But in that case, when the user edits the text field, the variable will have the text field's text. Now if the user decides to pick the first value again, the new value is not reflected in the variable.

如何克服这个问题?谢谢。

How can I overcome this? Thanks.

以下是相关代码

在我的 viewDidLoad 我有这个声明,默认情况下将 selectText 设置为选择器的第一个选项

In my viewDidLoad I have this statement to set the selectText to first option of the picker by default

[self setSelectText:[myArray objectAtIndex:0]];

textFieldShouldReturn

- (BOOL)textFieldShouldReturn:(UITextField *)txtField{
    [txtField resignFirstResponder];
    [self setSelectText:txtField.text];
    return YES;
}

PickerViewDelegate的didSelectRow

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // report the selection to the UI label
    [self setSelectText:[myArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
}

问题出现在用户编辑文本然后决定他毕竟想要的时候使用选择器的第一个值。在这种情况下,他们将关闭键盘并单击选择器的第一个选项(已选中)。此操作不会调用 didSelectRow ,因此selectText的值不会更改。

The problem arises when the user edits the text and then decides he after all wants to use the first value of the picker. In that case they will dismiss the keyboard and click on the 1st option of the picker (which is already selected). This action does not call didSelectRow and so the value of selectText is not changed.

为了使用户能够能够更改 selectText 的值,他们首先必须从选择器中选择一些其他值,然后选择第一行。

In order for the user to be able to change the value of selectText, they first have to select some other value from the picker and then select the 1st row.

推荐答案

每次使用此代码进行选择时,不要推选择器的值:

Instead of "pushing" the value of the picker each time you make a selection with this code:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

当用户点击按钮(或您正在使用的其他事件)时,您应该拉它,使用类似于:

you should "pull it" when the user clicks the button (or other event you are using) using something similar to:

- (void)myAction:(id)sender
{

    NSInteger row = [myPicker selectedRowInComponent:0];
    selectedText = [myArray objectAtIndex:(NSUInteger)row];


}

清除文字: UITextField class提供了一个用于清除当前文本的内置按钮。因此,如果用户不想要该文本,他可以将其丢弃。

For clearing the text: The UITextField class provides a built-in button for clearing the current text. So if the user does not want that text he can discard it.

myTextField.clearButtonMode = UITextFieldViewModeAlways; 

所以在脚本中,你会检查 TextField 有一个值,如果它有值,你使用TextField,如果它没有值,你从 Picker 中提取信息。

So in the script, will you check if the TextField has a value, if it does have a value u use the TextField, if it doesn't have a value, you pull the information from the Picker.

这篇关于UIPickerView - 第一行选择不会调用didSelectRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆