Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action when UIDatePicker started being used

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.

like image 973
Kashif Avatar asked Oct 29 '22 11:10

Kashif


1 Answers

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.

like image 166
Frankie Avatar answered Nov 11 '22 06:11

Frankie