简体   繁体   English

Apple Swift Datepicker作为键盘

[英]Apple Swift Datepicker as keyboard

I am trying to show a date picker when a text control is clicked using Swift. 我正在尝试使用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. 我不知道发生了什么,它一直给我一个无效的选择器异常,有人知道了吗,我知道Swift是非常新的。

You should wrap the action parameter with the Selector initializer: 您应该使用Selector初始化程序包装action参数:

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. 我正在从您的代码中逐字复制它,但是“句柄”似乎拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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