*仅*从 UITextView 观察键盘通知?将 UITextView 传递给 "object" 参数无效

Observe keyboard notifications *only* from UITextView? Passing UITextView to "object" parameter isn't working

假设一个视图控制器同时包含一个 UITextView 和一个 UITextField。如果目标是观察来自 UITextView 的键盘通知,下面的代码不应该起作用吗?

不幸的是,它不起作用。与通知关联的函数永远不会被调用。有没有办法只从 UITextView 观察键盘通知?

之所以重要是因为我们需要为 UITextView 而不是 UITextField 滚动父 UIView。否则,键盘会遮挡 UITextView。

class CustomViewController: UIViewController {

    @IBOutlet weak var promptField: UITextView!
    @IBOutlet weak var nameField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get notified when keyboard appears
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: promptField)
    }

    func keyboardWillShow(notification: NSNotification) {
        // Do stuff
    }
}
NSNotificationCenter.defaultCenter().addObserver(self, 
    selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, 
    object: promptField)

该代码的结果将是不发送通知。 object 永远不会 成为 promptField。对象是发送者,显示键盘和发送通知的不是字段——它是应用程序。

想问的问题大概是谁是第一响应者...