Bossvanlindt Bossvanlindt - 6 months ago 44
Swift Question

UIDatePicker doesn't recognise that it currently shows the current date

My swift app uses a UIDatePicker for the user to select a date. However, I want to set it up so that it displays "No date inserted" for when the user forgets to insert it. The problem is that when I set up the if statement for showing that if the datePicker's date is equal to the current date, it should display the warning, but it simply doesn't recognise it at all. Here's my code:

if == NSDate() {
warnL.text = "NO DATE INSERTED"

What could I do about it?
Any help would be very appreciated! Thanks.


NSDate return all date, including time, so it will never be equal to time in Your datePicker. You can define variable which tell You if date was changed:

var dateWasChanged = false

And then You can register handler for change event:

func viewDidLoad() {
   datePicker.addTarget(self, action: #selector(YourViewController.handleDatePicker), forControlEvents: UIControlEvents.ValueChanged)

func handleDatePicker() {
    dateWasChanged = true

After that You can check if dateWasChanged is true and show Your information.