Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker upon date changed

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?

like image 814
Josh Kahane Avatar asked Sep 24 '10 09:09

Josh Kahane


People also ask

How do you present a date picker?

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.

What is UIDatePicker?

A control for inputting date and time values.

How do I change the appearance of the uidatepicker?

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.

Are there other options for selecting dates and times in uidatepicker?

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.

What happened to the uidatepicker on iOS?

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.

Does uidatepicker inherit from uipickerview?

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.


2 Answers

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 } 
like image 134
Vladimir Avatar answered Sep 23 '22 21:09

Vladimir


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()

like image 27
John Riselvato Avatar answered Sep 21 '22 21:09

John Riselvato