I have just started using UIDatePicker in my iPad app, but is there a way of checking when the date has been changed?
I want to do something when the date is changed. What can I try?
On an Access form, use the Date Picker to enter the current date. If the field is set up as a Date/Time field, the Date Picker icon appears when you click in the field. Click the icon, and then click the Today button below the calendar.
A control for inputting date and time values.
For information about the date picker’s inherited Interface Builder attributes, see UIControl and UIView. You can change the appearance of UIDatePicker by setting preferredDatePickerStyle. For a list of appearance styles, see UIDatePickerStyle. You should integrate date pickers in your layout using Auto Layout.
There are now some choices other than just the slot machine-style wheels! Since iOS 2.0, the original style of UIDatePicker has been the only standard UIKit option for developers looking to let users select dates and times.
The UIDatePicker got a little bit of a facelift with the new styles in iOS 7+ but really hasn’t had much love since then. With iOS 13.4 and iOS 14.0, things changed with the addition of the UIDatePickerStyle enumeration and the addition of two new styles other than the default wheels.
UIDatePicker does not inherit from UIPickerView, but it manages a custom picker-view object as a subview. You can set the minimum and the maximum date that UIDatePicker can show.
When properly configured, a UIDatePicker object sends an action message when a user finishes rotating one of the wheels to change the date or time; the associated control event is UIControlEventValueChanged.
so you need to add your class to handle UIControlEventValueChanged
event in your picker:
[picker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; ... - (void) dateChanged:(id)sender{ // handle date changes }
Swift 4 version:
UIDatePicker Setup:
let datePicker = UIDatePicker() datePicker.datePickerMode = .date dateTextField.inputView = datePicker datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
Function:
@objc func datePickerChanged(picker: UIDatePicker) { print(picker.date) }
If you notice, Swift 4 requires @objc in the function if you're going to use #selector()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With