1

我正在编写一个自定义键盘扩展,虽然我注册为键盘隐藏/显示通知的观察者,但我的断点从未命中,我的日志语句也从未打印。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")
}

}
4

0 回答 0