3

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

Braver Chiang
  • 351
  • 2
  • 13
  • I’ve edited the question, if it is better to understand now – Braver Chiang Dec 12 '17 at 11:24
  • Possible duplicate of [Getting selected value of a UIPickerViewControl in Swift](https://stackoverflow.com/questions/26674399/getting-selected-value-of-a-uipickerviewcontrol-in-swift) – Tamás Sengel Dec 12 '17 at 11:25
  • There is no supported way of doing this (check the [UIPickerViewDelegate](https://developer.apple.com/documentation/uikit/uipickerviewdelegate)). For a hackish approach, see [this answer](https://stackoverflow.com/a/7626141/1305067). – paulvs Dec 12 '17 at 11:47
  • @the4kman What I need is different from your link, I have edited the question – Braver Chiang Dec 12 '17 at 11:59

2 Answers2

2

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), 
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • I’m using UIPickerView, Thank you for the answer, I’ve implemented **get the data by click a Done button** before, but I can also get the data when I’m **scrolling**, but can’t put data into the Label when I’m scrolling – Braver Chiang Dec 12 '17 at 11:43
  • Did you put your label update code in the pickerView didSelectRow function? You might need to dispatch it on the MainQueue to see it update correctly. – davidethell Dec 12 '17 at 11:51
  • I will try it later – Braver Chiang Dec 12 '17 at 12:07
0

Let me suppose that you are using UIDatePicker, in that you can control the action using UIControlEventValueChanged

Like,

datePickerView?.addTarget(self, action: #selector(self.valueChanged(_:)), for: UIControlEvents.valueChanged)

the valueChanged() will be,

func valueChanged(_ datePicker: UIDatePicker) {
    let selectedDate = datePicker.date as NSDate
    print(selectedDate)
}

and if you are using UIPickerview then,

titleForRow: will gave you scrolling value

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    print("your value")
}

and didSelectRow: will give you selected value

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
     print("your value")
}
Bhavi Lad
  • 237
  • 2
  • 7