我在文本字段输入文本后,每次都会崩溃。我试图在输入文本后将其保存并尝试触摸另一个文本域进行编辑,但它只是冻结。我正在尝试使用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()
}
}
答案 0 :(得分:0)
尽量不要使用!
进行投射,否则会导致应用崩溃。这是一个使用可选值的快速修补程序,但未经过测试。如果这有助于你,你能否回答:
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()