How To Get Selected Value From UIPickerView

后端 未结 10 2208
梦如初夏
梦如初夏 2020-12-04 11:41

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 c

相关标签:
10条回答
  • 2020-12-04 12:37

    You can get it in the following manner:

    NSInteger row;
    NSArray *repeatPickerData;
    UIPickerView *repeatPickerView;
    
    row = [repeatPickerView selectedRowInComponent:0];
    self.strPrintRepeat = [repeatPickerData objectAtIndex:row];
    
    0 讨论(0)
  • 2020-12-04 12:42

    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

    0 讨论(0)
  • 2020-12-04 12:42

    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.

    0 讨论(0)
  • 2020-12-04 12:43
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
       weightSelected = [pickerArray objectAtIndex:row];
    }
    
    0 讨论(0)
提交回复
热议问题