ios 如何从 UIPickerView 获取选定的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5725580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Get Selected Value From UIPickerView
提问by SJS
I know that with a UIDatePicker, you can use something like:
我知道使用 UIDatePicker,您可以使用以下内容:
NSDate *myDate = picker.date;
But I am using a UIPickerView in my view. How can i similarly get the value selected? Or do I have to setup didSelectRow type of method to do this?
但我在我看来使用 UIPickerView 。我怎样才能类似地获得选定的值?或者我是否必须设置 didSelectRow 类型的方法才能做到这一点?
Update: This code works for picker with 1 component:
更新:此代码适用于具有 1 个组件的选择器:
NSInteger row;
NSString *weightSelected;
row = [repPicker selectedRowInComponent:0];
weightSelected = [pickerArray objectAtIndex:row];
I tired this code for my picker with 2 components, but it is freezing:
我用 2 个组件为我的选择器使用了这段代码,但它冻结了:
NSInteger row1, row2;
NSString *weightSelected1;
NSString *weightSelected2;
row1 = [repPicker selectedRowInComponent:0];
row2 = [repPicker selectedRowInComponent:1];
weightSelected1 = [pickerArray objectAtIndex:row1];
weightSelected2 = [pickerArray objectAtIndex:row2];
NSString *weightSelected = [NSString stringWithFormat:@"%@.%@", weightSelected1, weightSelected2];
回答by SJS
You can get it in the following manner:
您可以通过以下方式获取它:
NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;
row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];
回答by nh32rg
You can get the text of the selected item in any section of the picker using the same function that the pickerView does, from your custom ViewController class:
您可以使用与 pickerView 相同的功能从您的自定义 ViewController 类中获取选择器任何部分中所选项目的文本:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
using the selected item
使用所选项目
[myPickerView selectedRowInComponent:0]
so to set the text of a label in a custom cell using the currently selected value from the 2nd section of myPickerView (a property of your view controller probably)
因此,使用 myPickerView 的第二部分中当前选择的值(可能是您的视图控制器的属性)在自定义单元格中设置标签的文本
[cell.label setText:[self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]];
Just change both :1s to :2s, etc for each section
只需将每个部分的 :1s 更改为 :2s 等
回答by Harikrishnan
This is what I did:
这就是我所做的:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedEntry = [allEntries objectAtIndex:row];
}
The selectedEntry
is a NSString
and will hold the currently selected entry in the pickerview. I am new to objective C but I think this is much easier.
该selectedEntry
是NSString
和将持有pickerview当前的选择项。我是目标 C 的新手,但我认为这要容易得多。
回答by drewag
You can access the selected row for a given component using the following method:
您可以使用以下方法访问给定组件的选定行:
- (NSInteger)selectedRowInComponent:(NSInteger)component
Otherwise, implementing the delegate function is the only other option.
否则,实现委托功能是唯一的其他选择。
回答by J-Dizzle
You will need to ask the picker's delegate, in the same way your application does. Here is how I do it from within my UIPickerViewDelegate:
您将需要以与您的应用程序相同的方式询问选择器的委托。以下是我在 UIPickerViewDelegate 中执行此操作的方法:
func selectedRowValue(picker : UIPickerView, ic : Int) -> String {
//Row Index
let ir = picker.selectedRow(inComponent: ic);
//Value
let val = self.pickerView(picker,
titleForRow: ir,
forComponent: ic);
return val!;
}
回答by pkamb
Getting the selected title of a picker:
获取选择器的选定标题:
let component = 0
let row = picker.selectedRow(inComponent: component)
let title = picker.delegate?.pickerView?(picker, titleForRow: row, forComponent: component)
回答by maan89
NSInteger SelectedRow;
SelectedRow = [yourPickerView selectedRowInComponent:0];
selectedPickerString = [YourPickerArray objectAtIndex:SelectedRow];
self.YourTextField.text= selectedPickerString;
// if you want to move pickerview to selected row then
for (int i = 0; I<YourPickerArray.count; i++) {
if ([[YourPickerArray objectAtIndex:i] isEqualToString:self.YourTextField.text]) {
[yourPickerView selectRow:i inComponent:0 animated:NO];
}
}
回答by Dave DeLong
You have to use the didSelectRow
delegate method, because a UIPickerView
can have an arbitrary number of components. There is no "objectValue" or anything like that, because that's entirely up to you.
您必须使用didSelectRow
委托方法,因为 aUIPickerView
可以有任意数量的组件。没有“objectValue”或类似的东西,因为这完全取决于你。
回答by cHELSEA
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
weightSelected = [pickerArray objectAtIndex:row];
}
回答by Ahmad Alwisi
This is my answer
这是我的回答
- (IBAction)Result:(id)sender
{
self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];
}