ios - 在敲击键盘键后触发keyboardDidShowNotification 和keyboardWillShowNotification
问题描述
这是整个应用程序的代码。只需UITextField
在情节提要上添加一个并点击它。您将打印“AAAA”。然后点击键盘上的任意键。它将再次打印“AAAA”。
为什么在这种情况下会第二次触发 UIResponder.keyboardDidShowNotification 通知?我怎样才能防止这种情况?
keyboardWillShowNotification 的行为相同 :(
代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addKeyboardObservers(to: .default)
}
func addKeyboardObservers(to notificationCenter: NotificationCenter) {
notificationCenter.addObserver(
forName: UIResponder.keyboardDidShowNotification,
object: nil,
queue: OperationQueue.main,
using: { _ in
print("AAAA")
}
)
}
}
Harish 的代码也一样:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWasShown),
name: UIWindow.keyboardDidShowNotification,
object: nil)
}
@objc func keyboardWasShown(_ notification: NSNotification) {
print("AAA")
}
}
解决方案
为什么不在下面使用?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWasShown),
name: UIWindow.keyboardDidShowNotification,
object: nil)
}
@objc func keyboardWasShown(_ notification: NSNotification) {
}
}
推荐阅读
- java - 如何在spring-boot数据休息的POST json中传递@EmbeddedId
- google-apps-script - Apps 脚本错误 - 找不到方法 getRange(number,number,(class),number)
- c# - 从单个路径删除级联删除
- javascript - 当它们准备好时,使用 angular 中的 observable 值
- python - 使用 selenium 自动爬取
- node.js - 我无法点击 X 射线刮刀中的链接并获取数据
- python - pyhive:使用 pyhive 设置 hive 属性
- php - 注册表单错误处理:记住正确的字段
- html - 使用 CSS 的 div 前后有不同的颜色
- ios - 空对象阻止可解码的 JSON 快速工作