Это код для всего приложения. Просто добавьте UITextField
на раскадровку и нажмите на нее. Вы напечатаете «AAAA». Затем нажмите на любую клавишу на клавиатуре. Он снова напечатает «AAAA».
Почему уведомление UIResponder.keyboardDidShowNotification запускается 2 раза в этой ситуации? Как я могу предотвратить это?
То же поведение для клавиатурыWillShowNotification: (
КОД:
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")
}
)
}
}
то же самое для кода из Hari sh:
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")
}
}