[英]Get selected picker view component string value
I have a picker view and it has four components. 我有一个选择器视图,它包含四个组件。 I have created a label that is displayed.
我创建了一个显示的标签。 I have a two array populating the first component (component 0) label text, and the last component (component 3) label text.
我有两个数组,分别填充第一个组件(组件0)标签文本和最后一个组件(组件3)标签文本。 For the center two components, I have the label text set like this
对于中心两个组件,我将标签文本设置如下
label.text = [NSString stringWithFormat:@"%d", row];
. 。
NOTE: this is in the viewForRow:
method. 注意:这在
viewForRow:
方法中。 I have a button below it that I want to get selected data from each component. 我下面有一个按钮,我想从每个组件中获取选定的数据。 How can I get the string value of the selected center two components?
如何获取所选中心两个组成部分的字符串值?
I know how to get the two with arrays but I'm unsure how to get the selected label text. 我知道如何将两个与数组,但我不确定如何获取所选的标签文本。 Forgive me if there's some very simple answer I have just missed or overlooked.
如果有一个非常简单的答案我已经错过或忽略了,请原谅我。
You should implement the delegate method, pickerView:didSelectRow:inComponent:, which will give you the row and component that was selected. 您应该实现委托方法pickerView:didSelectRow:inComponent :,该方法将为您提供选定的行和组件。 You can use that information to access the correct index in the appropriate array.
您可以使用该信息来访问适当数组中的正确索引。
- (void)pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger) component {
rowValue = row;
}
just keep the row index in global attribute then get the value like this 只需将行索引保留在global属性中,然后获取像这样的值
NSStrin *string=[NSString stringWithFormat:@"%d", rowValue];
On selecting the button just get the current picker selected row 在选择按钮时,只需选择当前选择器行
NSInteger row = [yourPicker selectedRowInComponent:0];
//Choose it for other components
NSString *yourComponentValue = [yourArray objectAtIndex:row];
Or 要么
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *yourComponentValue = [NSString stringWithFormat:@"%d",[yourarray objectAtIndex:row];
//Imagine this method will be called whenever the user scrolls. Usability point-of-view i would suggest not to update any of your UI here
}
This UPicker delegate method will be called when the user scrolls the picker and the scrolling is finished. 当用户滚动选择器并且滚动完成时,将调用此UPicker委托方法。
Choose where and when you want it to be 选择您希望在何时何地
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.