我正在编写一个自定义键盘扩展,虽然我正在注册作为键盘隐藏/显示通知的观察者,我的断点从未命中,我的日志语句永远不会打印。在UIInputViewController子类中是否存在与NSNotifications不同的事情,或者我做错了什么?
import UIKit
class KeyboardViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
let keysView:UIView = NSBundle.mainBundle().loadNibNamed("KeyboardView", owner: self, options:nil)[0] as UIView
keysView.frame = self.inputView.frame
keysView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.inputView.addSubview(keysView)
self.inputView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[keysView]|", options: nil, metrics: nil, views:["keysView":keysView]))
self.inputView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[keysView]|", options: nil, metrics: nil, views:["keysView":keysView]))
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Listen for changes to keyboard visibility so that we can adjust the view accordingly.
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "handleKeyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: "handleKeyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func handleKeyboardWillHideNotification(notification: NSNotification) {
NSLog("keyboardWillHide")
}
func handleKeyboardWillShowNotification(notification: NSNotification) {
NSLog("keyboardWillShow")
}
}