如何从UIPickerView中获取选定值

时间:2022-12-03 10:05:12

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:

我厌倦了这段代码,因为我的拾音器有两个组件,但是它太冷了:

    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];

8 个解决方案

#1


123  

You can get it in the following manner:

您可以通过以下方式获得:

NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;

row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];

#2


32  

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所做的相同功能,在picker的任何部分中获取所选项的文本:

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

只要把每个部分的1换成2s,等等

#3


6  

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中保存当前选中的条目。我是objective - C的新手,但我觉得这容易得多。

#4


5  

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.

否则,实现委托函数是惟一的其他选项。

#5


1  

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委托方法,因为UIPickerView可以有任意数量的组件。没有“objectValue”之类的东西,因为这完全取决于您。

#6


0  

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:

您将需要以与应用程序相同的方式询问picker的委托。这是我如何在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!;
}

#7


-2  

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   weightSelected = [pickerArray objectAtIndex:row];
}

#8


-5  

This is my answer

这是我的回答

- (IBAction)Result:(id)sender 
{
   self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];

}

#1


123  

You can get it in the following manner:

您可以通过以下方式获得:

NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;

row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];

#2


32  

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所做的相同功能,在picker的任何部分中获取所选项的文本:

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

只要把每个部分的1换成2s,等等

#3


6  

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中保存当前选中的条目。我是objective - C的新手,但我觉得这容易得多。

#4


5  

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.

否则,实现委托函数是惟一的其他选项。

#5


1  

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委托方法,因为UIPickerView可以有任意数量的组件。没有“objectValue”之类的东西,因为这完全取决于您。

#6


0  

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:

您将需要以与应用程序相同的方式询问picker的委托。这是我如何在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!;
}

#7


-2  

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   weightSelected = [pickerArray objectAtIndex:row];
}

#8


-5  

This is my answer

这是我的回答

- (IBAction)Result:(id)sender 
{
   self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];

}