在macOS中,如何监测nstextfield更改

・1 分钟阅读

Storyboard

在Storyboard中,NSTextField有一个操作选项,用于指定Send on

代码

在代码中,当文本字段值更改时,NSTextFieldDelegate通知,当按下Enter键时,target action通知

import CocoaclassViewController: NSViewController, NSTextFieldDelegate{@IBOutletweakvar textField: NSTextField!overridefuncviewDidLoad() {super.viewDidLoad() textField.delegate = self }funccontrolTextDidChange(_ obj: Notification) {let textField = obj.object as! NSTextFieldprint(textField.stringValue) }}

使用EasyClosure

如果使用EasyClosure,会更容易

let textField: NSTextField =textField.on.action { string inprint("User has pressed enter (string)"}textField.on.change { string inprint("Text field value has changed")}
讨论
Xiyao739 profile image