Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Swift Datepicker as keyboard

Tags:

swift

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.

like image 703
Ksliman Avatar asked Jun 11 '14 02:06

Ksliman


1 Answers

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.

like image 174
Erik Avatar answered Sep 30 '22 17:09

Erik