【发布时间】:2019-02-16 22:02:47
【问题描述】:
我有一个函数触发两个观察者 - keyboardWillShowNotification 和 KeyboardWillHideNotification,我基本上所做的是在编辑时重置文本字段,模拟 clearsOnBeginEditing 的效果。但是,如果用户将该字段留空,我希望它在编辑之前返回原始值。这是我的实验代码:
@objc func keyboardWillChange(notification: Notification) {
if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillHideNotification {
if textField.isEditing {
let originalValue: String!
originalValue = textField.text!
textField.text = ""
print("content while editing is \(originalValue)")
if notification.name == UIResponder.keyboardWillHideNotification {
print("this is content on end editing \(originalValue)")
if textField!.isEmpty == true {
print("this is nill")
textField.text! = originalValue
} else {
textField.text = textField.text
print("found a value")
}
}
}
}
正确的做法是什么?
【问题讨论】:
标签: swift textfield nsnotifications uiresponder