5

I want to trigger an action, as soon as the user starts to change the value of the UIDatePicker. My first instinct was to use the datePickerEditingDidBegin method but does not seem to do the job.

Kashif
  • 4,642
  • 7
  • 44
  • 97
  • Hi, did you come out with any solution for triggering action on starting being used? – joan Apr 11 '19 at 10:33

2 Answers2

2

Straight from the docs:

UIDatePicker: Responding to User Interaction

Date pickers use the Target-Action design pattern to notify your app when the user changes the selected date. To be notified when the date picker’s value changes, register your action method with the valueChanged event. At runtime the date picker calls your methods in response to the user selecting a date or time.

You connect a date picker to your action method using the addTarget(_:action:for:) method or by creating a connection in Interface Builder. The signature of an action method takes one of three forms, which are listed in Listing 1. Choose the form that provides the information that you need to respond to the value change in the date picker.

Listing 1 Action methods for date pickers

@IBAction func doSomething()
@IBAction func doSomething(sender: UIDatePicker)
@IBAction func doSomething(sender: UIDatePicker, forEvent event: UIEvent)

Connect your date pickers valueChanged send event to your IBAction

enter image description here

Edit:

Per the docs, UIDatePicker doesn't have any delegate methods, therefore something like datePickerEditingDidBegin wouldn't make any sense.

You have to use UIControlEvent. If valueChanged doesn't suit your needs I would maybe suggest touchDragInside.

An event where a finger is dragged inside the bounds of the control.

Other UIControlEvent descriptions located here in the docs.

Community
  • 1
  • 1
Frankie
  • 11,508
  • 5
  • 53
  • 60
  • I know about valueChanged event, I am asking for event when the users **starts** to change the value of DatePicker but is not done yet. – Kashif Feb 27 '17 at 18:07
  • Strangely, even `TouchDragInside` doesn't get kicked either. BTW, I was using the `EditingDidBegin` earlier, which is towards the top of your list. – Kashif Feb 28 '17 at 13:29
  • 1
    You called also try overriding the `touchesBegan` method or adding a tap or pan gesture recognizer. – Frankie Feb 28 '17 at 16:11
0

This wasn't working for me too, I tried all UIEvent's like above. I was able to get this working by adding a pan gesture recognizer, conforming to the delegate and shouldRecognizeSimultaneouslyWith(: ) = true . See answer here: https://stackoverflow.com/a/25719326/4970749

bubbaspike
  • 101
  • 6