简体   繁体   中英

How To Get Selected Value From UIPickerView

I know that with a UIDatePicker, you can use something like:

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?

Update: This code works for picker with 1 component:

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

You can get it in the following manner:

NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;

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

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:

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

[cell.label setText:[self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]];

Just change both :1s to :2s, etc for each section

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.

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.

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:

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!;
}

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.

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)
       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];
 }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   weightSelected = [pickerArray objectAtIndex:row];
}

This is my answer

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM