简体   繁体   English

获取选定的选择器视图组件字符串值

[英]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. 您可以使用该信息来访问适当数组中的正确索引。

UIPickerView has a " selectedRowInComponent: " method which can help you. UIPickerView有一个“ selectedRowInComponent: ”方法可以为您提供帮助。 You can get some reference from this link too- 您也可以从此链接获得一些参考-

- (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.

相关问题 从选取器视图中获取选定值的功能 - Function to get selected value from Picker View 从选择器视图(选择器轮)中获取选定的值 - Get selected value from picker view (picker wheel) 无法从 Swiftui 中的 Picker 获取选定值 - Unable to get selected value from Picker in Swiftui 显示从选择器视图到集合视图单元格的特定标签的选定值 - display the selected value from picker view to particularlabel of a cell of collection view SwiftUI 选择器中所选选项的值不会更新视图 - Value of Selected Option From a SwiftUI Picker does not Update the View 在同一视图上填充与在第一个选择器视图中选择的值相对应的第二个选择器 - Populating second picker corresponding to the value selected in first picker view on same view 选择器视图值 - picker view value 获取func imagePickerController的返回值(选择器:UIImagePickerController,didFinishPackingWithMediaInfo:[String:AnyObject]) - Get return value of func imagePickerController(picker: UIImagePickerController, didFinishPackingWithMediaInfo: [String : AnyObject]) 没有在标签上的选择器视图中显示所选对象 - Not showing the selected object from picker view on the label SwiftUI 选择器 聚焦于视图中的选定项目 - SwiftUI Picker Focus on selected item in view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM