How To Get Selected Value From UIPickerView

后端 未结 10 2209
梦如初夏
梦如初夏 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: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

提交回复
热议问题