How To Get Selected Value From UIPickerView

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

    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.

    0 讨论(0)
  • 2020-12-04 12:20
           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];
     }
    }
    
    0 讨论(0)
  • 2020-12-04 12:27

    This is my answer

    - (IBAction)Result:(id)sender 
    {
       self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];
    
    }
    
    0 讨论(0)
  • 2020-12-04 12:33

    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.

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

    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!;
    }
    
    0 讨论(0)
  • 2020-12-04 12:35

    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)
    
    0 讨论(0)
提交回复
热议问题