简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM