How to Get Selected Value from Uipickerview

How To Get Selected Value From UIPickerView

You can get it in the following manner:

NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;

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

How to get the String value of selected row in a UIPickerView in Swift 3?

It's simple. Please use the data model for it.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

switch pickerView {
case sexPicker:
sexText.text = sex[row]
case agePicker:
ageText.text = age[row]
case proPicker:
professionText.text = profession[row]
default:
break
}

}

Get currently selected value of UIPickerView

Every UIPickerView should have a delegate.

And you can ask this delegate for your picker's selected row title via something like:

  UIPickerViewDelegate *delegate = yourPickerView.delegate;
NSString *titleYouWant = [delegate pickerView:yourPickerView titleForRow:[yourPickerView selectedRowInComponent:0] forComponent:0];

(This is ASSUMING your component number is zero; your own implementation might be different).

More documentation on the "pickerView:titleForRow:forComponent" method can be seen here.

Getting selected value of a UIPickerViewControl in Swift

you will have to set the picker view delegate to self and override this function

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// use the row to get the selected row from the picker view
// using the row extract the value from your datasource (array[row])
}

or

you can use this to extract as per your usage

var selectedValue = pickerViewContent[pickerView.selectedRowInComponent(0)]

where pickerViewContent is your array of dataSource

How can I get the selected value in the real-time, when I'm scrolling the UIPickerView

It is unclear if you are using a UIPickerView or a UIDatePicker.

For UIPickerView you need to implement the UIPickerViewDelegate. Make sure that delegate is added to your ViewController declaration and make sure in Storyboard to connect the delegate of the UIPickerView control to your view controller. Then implement this function:

func pickerView(_ pickerView: UIPickerView, 
didSelectRow row: Int,
inComponent component: Int) {

}

For UIDatePicker you need to connect the action of the UIDatePicker in Storyboard to an @IBAction function in your view controller or else connect it in code using the addTarget function:

myDatePicker.addTarget(self, action: #selector(self.respondToPicker, for: .valueChanged), 

UIPickerView get selected value without datasource

You can use pickerView.selectedRow(inComponent:)

let firstValue = pickerView.selectedRow(inComponent: 0)
let secondValue = pickerView.selectedRow(inComponent: 1)

Both will just be an Int value representing the currently selected row index in it's respective component. And since your values are starting from 0, you can use the values directly.

Get selected value from picker view (picker wheel)

Change test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])" to this: test.text = "\(possibleNums[row])"

You only have one component, so you don't need to check for it, just return the row number as the array's index.

I want to save a Selected Option in a Swift UIPickerView

You can simply save the selected row number in UserDefaults so that it can be accessed next time.

// Capture the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = measurementTypes[row]
// Save selected row integer in User Defaults
UserDefaults.standard.set(row, forKey: "pickerViewRow")
}

Then, in override func viewDidLoad() you can retrieve this value and set the picker view:

let row = UserDefaults.standard.integer(forKey: "pickerViewRow")
pickerView.selectRow(row, inComponent: 0, animated: False)


Related Topics



Leave a reply



Submit