UITextField with DatePicker crashes

时光总嘲笑我的痴心妄想 提交于 2020-01-17 06:53:32

问题


I am new to iOS/Swift so please bear with me.

I have a TextField, timeTextField, and want to display a TimePicker on click. All this is working property but when another element is selected the app crashes why does it crash? UPDATE 1: updated code code:

func setupTimePicker(){
    datePickerView.datePickerMode = UIDatePickerMode.time
    datePickerView.removeFromSuperview()
    timeTextField.inputView = datePickerView
    datePickerView.addTarget(self, action: #selector(self.datePickerValueChanged(sender:)), for: .valueChanged)
}

func datePickerValueChanged(sender:UIDatePicker) {
    var date = Date()
    let calendar = Calendar.current

    date = sender.date;

    let hour = calendar.component(.hour, from: date)
    let minutes = calendar.component(.minute, from: date)

    timeTextField.text = String(format: "%02d", hour) + ":" + String(format: "%02d", minutes)
}

error:

[TestApp.ViewController pickTime:]: unrecognized selector sent to instance 0x7f9305e08e20


回答1:


Since iOS8/iOS9, when you added a view as inputView to UITextField app crashes. To resolve your crash, before adding datePicker as inputView to UITextField call [<pickerView> removeFromSuperView]. This will solve crash issue.



来源:https://stackoverflow.com/questions/42927969/uitextfield-with-datepicker-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!