Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is my prototype For example, in this image, when I'm scrolling the UIPickerView to 2012 9 28, what I want is that the text of the black label will change into 2012 9 28 at the same time without pressing any buttons like the Done button

I’m using UIPickerView, I can get the selected data before, I can also put the data into label by clicking a Done button, but I cannot put the data into the label when I’m scrolling.

and In a general situation, My question is that when I Scroll the UIPickerView, how can I get the data which is selected in real time

could anyone help me ? ObjectiveC solution is OK, Swift solution is better for me, Thank you so much

like image 236
Braver Chiang Avatar asked Dec 10 '25 15:12

Braver Chiang


1 Answers

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), 
like image 150
davidethell Avatar answered Dec 13 '25 04:12

davidethell