Ksliman Ksliman - 5 months ago 33
Swift Question

Apple Swift Datepicker as keyboard

I am trying to show a date picker when a text control is clicked using Swift.

I have done this like so:

var DatePickerView: UIDatePicker = UIDatePicker()
DatePickerView.datePickerMode = UIDatePickerMode.Date
txtBirthdate.inputView = DatePickerView

Now when the user taps the text box instead of a keyboard we get a date picker, the problem is with this line:

DatePickerView.addTarget(self, action: "handelDatePicker", forControlEvents: UIControlEvents.ValueChanged)

And I handle the control like so:

func handelDatePicker()
txtBirthdate.text = dateFormatter.stringFromDate(DatePickerView.date)

I don't know what is going on, it keeps giving me an invalid selector exception, has anyone figured this out yet, I know Swift is very new.

Answer Source

You should wrap the action parameter with the Selector initializer:

DatePickerView.addTarget(self, action: Selector("handelDatePicker"), forControlEvents: UIControlEvents.ValueChanged)

I'm copying it verbatim here from your code, but also 'handle' seems to be misspelled.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download