gpt4 book ai didi

swift 多个 UITextField EditingDidEnd 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 11:34:59 25 4
gpt4 key购买 nike

每次我在文本字段中输入文本后,我的应用程序都会崩溃。我试图在输入文本后保存它,并尝试触摸另一个文本字段进行编辑,但它只是卡住了。我正在尝试使用 textFieldDidEndEditing 或 EditingDidEnd 作为文本字段。任何帮助将非常感激。这是我的发言。

//MARK: Text selected field delegate
extension ExerciseViewController: UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
var currentTextField = textFields[0]
if (currentTextField == textField) {

currentTextField = textFields[1]

currentTextField.becomeFirstResponder()
}
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print("User is editing text field")
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
let exercise = Exercise(context: context)
let client = Client(context: context)
exercise.reps = Int16(todayRepsTextField.text!)!
exercise.weight = Float(todayWeightTextField.text!)!
exercise.notes = todayNotesTextView.text
exercise.seat = Int16(seatPositionTextField.text!)!
exercise.nextWeight = Float(nextWeightTextField.text!)!
client.goals = goalsTextField.text
client.intensity = intensityTextField.text
client.medical = medicalTextField.text
client.notes = clientNotesTextField.text
exercise.exerciseInfo?.exerciseProtocol?.protocolName = protocolTextField.text
thisAppDelegate().saveContext()
}

}

最佳答案

尽量不要使用 ! 进行转换,它可能会使应用程序崩溃。这是使用可选值的快速修复,但未经测试。

let exercise = Exercise(context: context)
exercise.reps = Int16(todayRepsTextField.text ?? "0") ?? 0
exercise.weight = Float(todayWeightTextField.text ?? "0") ?? 0
exercise.notes = todayNotesTextView.text
exercise.seat = Int16(seatPositionTextField.text ?? "0") ?? 0
exercise.nextWeight = Float(nextWeightTextField.text ?? "0") ?? 0

let client = Client(context: context)
client.goals = goalsTextField.text
client.intensity = intensityTextField.text
client.medical = medicalTextField.text
client.notes = clientNotesTextField.text

exercise.exerciseInfo?.exerciseProtocol?.protocolName = protocolTextField.text
thisAppDelegate().saveContext()

关于swift 多个 UITextField EditingDidEnd 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com