如何通过单击按钮获取UIPickerview的选定值 [英] How to get the selected value of a UIPickerview from a button click
问题描述
在我看来,我有2个文本字段,1个标签,1个选择器和1个按钮
In my view I have 2 TextFields, 1 Label, 1 Picker and 1 button
我希望标签具有2个文本字段和选定的值
I want to the label to have the value of the 2 textfields and the selected value of the picker when the button is clicked.
以下是我的按钮单击代码
The following is my button click code
-(IBAction)changeTheTextOfTheLabel
{
NSInteger row;
row = [listPicker selectedRowInComponent:0];
NSString *fullName = [[NSString alloc] initWithFormat:@"%@ %@ (%@)",firstNameTextView.text,lastNameTextView.text, [listArray objectAtIndex:row]];
[fullNameLabel setText:fullName];
[fullName release];
}
上面的代码可以正常工作,只不过它始终将选择器的第一项放在标签,无论选择了哪一行。
The above code work fine except that it always put the first item ofthe picker in the label no matter which row it was selected.
谢谢。
推荐答案
实例变量 listPicker
已连接到IB中的 UIPickerView
。
It doesn't look like the instance variable listPicker
is connected to the UIPickerView
in IB.
row = [listPicker selectedRowInComponent:0];
此处 row
如果 listPicker
是 nil
,只有当 listPicker
不是已连接。
Here row
will always be zero if listPicker
is nil
and that should happen only when listPicker
is not connected.
这篇关于如何通过单击按钮获取UIPickerview的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!