Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Does controlTextDidChange Work With NSTextView?

I have one text field and one textView. The controlTextDidChange method responds to a text field change. But it doesn't respond to a textview change.

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")
        }
    }
}

I'm running Xcode 7.2.1 under Yosemite. Am I doing anything wrong?

like image 401
El Tomato Avatar asked May 14 '16 06:05

El Tomato


1 Answers

controlTextDidChange: is a method of NSControl class which is inherited by NSTextField, but not by NSTextView:

enter image description here

Because of that, instances of NSTextView can't receive the above callback.

I think you should implement textDidChange: from NSTextViewDelegate protocol instead:

func textDidChange(notification: NSNotification) {
  if notification.object == textView {
    print("good")
  }
}
like image 148
Ozgur Vatansever Avatar answered Nov 02 '22 04:11

Ozgur Vatansever