Swift - controlTextDidChange是否可以与NSTextView一起使用?

El *_*ato 5 cocoa nstextview swift

我有一个文本字段和一个textView.controlTextDidChange方法响应文本字段更改.但它没有回应textview的变化.

class AppDelegate: NSObject,NSApplicationDelegate,NSTextFieldDelegate,NSTextViewDelegate {
    func applicationWillFinishLaunching(notification:NSNotification) {
        /* delegates */
        textField.delegate = self
        textView.delegate = self    
    }

    override func controlTextDidChange(notification:NSNotification?) {
        if notification?.object as? NSTextField == textField {
            print("good")
        }
        else if notification?.object as? NSTextView == textView {
            print("nope")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在Yosemite下运行Xcode 7.2.1.我做错了吗?

ozg*_*gur 7

controlTextDidChange:是一个NSControl类的方法,它由NSTextField,但不是由NSTextView以下内容继承:

在此输入图像描述

因此,NSTextView的实例无法接收上述回调.

我认为你应该textDidChange:从NSTextViewDelegate协议实现:

func textDidChange(notification: NSNotification) {
  if notification.object == textView {
    print("good")
  }
}
Run Code Online (Sandbox Code Playgroud)