我正面临一个奇怪的行为,文本字段成为第一响应者。我在ViewWillAppear中添加了键盘观察器,并在viewWillDisappear中将其删除。每当我不使textField成为第一响应者时,都会调用ViewController的Deinit,但是每当TextField成为第一响应者时,在关闭键盘时都不会调用Deinit。 我知道这很不寻常,但是有人面对过吗?这就是代码的样子-
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(ZLOTPBaseVC.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ZLOTPBaseVC.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(NSNotification.Name.UIKeyboardWillShow)
NotificationCenter.default.removeObserver(NSNotification.Name.UIKeyboardWillHide)
}