xcode 无法识别的选择器发送到实例 0x7f90f1f04180
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39317298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unrecognized selector sent to instance 0x7f90f1f04180
提问by iceman
I'm a newbie to swift. I'm using xcode version 8 beta 4. I'm having an error whenever I scroll through my Date Picker. I have read a very similar problem, but the answer there is not fixing my problem. Below is my code:
我是 swift 的新手。我使用的是 xcode 版本 8 beta 4。每当我滚动我的日期选择器时都会出错。我已经阅读了一个非常相似的问题,但那里的答案并没有解决我的问题。下面是我的代码:
func textFieldDidBeginEditing(_ textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: Selector("datePickerChanged:"), for: .valueChanged)
}
func datePickerChanged(sender: UIDatePicker){
let formatter = DateFormatter()
formatter.dateStyle = .long
dateLog.text = formatter.string(from: sender.date)
}
when I click on my textfield the UIDatePicker
is showing up just fine, but when I start to scroll through the dates, it gives me the following error:
当我点击我的文本字段时,UIDatePicker
它显示得很好,但是当我开始滚动日期时,它给了我以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyApp.ViewController datePickerChanged:]: unrecognized selector sent to instance 0x7f90f1f04180'
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MyApp.ViewController datePickerChanged:]:无法识别的选择器发送到实例 0x7f90f1f04180”
回答by Sweeper
Selector(String)
is deprecated. You should start using the new syntax #selector
.
Selector(String)
已弃用。您应该开始使用新语法#selector
。
Also, the colon at the end is not needed.
此外,不需要末尾的冒号。
So, your code should look like this:
所以,你的代码应该是这样的:
datePicker.addTarget(self, action: #selector(datePickerChanged),
for: .valueChanged)
回答by hariszaman
- change the
Selector
to#selector
- change
for
toforControlEvents
change
.valueChanged
to.ValueChanged
datePicker.addTarget(self, action: #selector(yourClass.datePickerChanged), forControlEvents: .ValueChanged)
- 更改
Selector
为#selector
- 更改
for
为forControlEvents
更改
.valueChanged
为.ValueChanged
datePicker.addTarget(self, action: #selector(yourClass.datePickerChanged), forControlEvents: .ValueChanged)