Text change notification for an NSTextField

前端 未结 5 675
不知归路
不知归路 2020-12-08 03:58

I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored

5条回答
  •  一整个雨季
    2020-12-08 04:41

    You should use NSTextFieldDelegate and implement controlTextDidChange. Test in macOS 10.14 and Swift 4.2

    import Cocoa
    
    class ViewController: NSViewController, NSTextFieldDelegate {
    
      @IBOutlet weak var textField: NSTextField!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        textField.delegate = self
      }
    
      func controlTextDidChange(_ obj: Notification) {
        let textField = obj.object as! NSTextField
        print(textField.stringValue)
      }
    }
    

提交回复
热议问题