UITextField with DatePicker crashes

2019-09-14 18:17发布

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条回答
虎瘦雄心在
2楼-- · 2019-09-14 18:33

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.

查看更多
登录 后发表回答