简体   繁体   English

如何在键入时立即更改NSTextField的值?

[英]How to change value of the NSTextField instantly as we type?

There are two outlets connected to my ViewController.swift file. 我的ViewController.swift文件有两个插座。

@IBOutlet var myOutlet: NSTextField!
@IBOutlet var yourOutlet: NSTextField!

There is an action connected too. 也有一个动作连接。

@IBAction func myAction(sender: NSTextField) {
    self.yourOutlet.doubleValue = myOutlet.doubleValue - 10

When i run the app, it works. 当我运行该应用程序时,它可以工作。 I enter 15 to text field that myOutlet connected, then hit the enter. 我在myOutlet连接的文本字段中输入15,然后按Enter。 And field that yourOutlet connected, takes the value of 5. yourOutlet连接的字段的值为5。

If there is no enter button needed, it would be better. 如果不需要输入按钮,那会更好。 So how to make the value change instantly, as we type? 那么,如何在键入时使值立即变化?

Thanks for the answers. 感谢您的回答。

I did not get a chance to do swift and OSX development, but I can lead you to the solution. 我没有机会进行快速和OSX开发,但是我可以带您找到解决方案。

Take a look at textDidChange(_:) 看一下textDidChange(_ :)

Posts a notification that the text has changed and forwards this message to the receiver's cell if it responds. 发布文本已更改的通知,如果响应,则将此消息转发到接收者的单元格。

Declaration 宣言

func textDidChange(_ aNotification: NSNotification) Parameters aNotification The NSControlTextDidChangeNotification notification that is posted to the default notification center. func textDidChange(_ aNotification: NSNotification)参数aNotification将NSControlTextDidChangeNotification通知发布到默认通知中心。 Discussion This method causes the receiver's delegate to receive a controlTextDidChange: message. 讨论此方法使接收方的委托接收controlTextDidChange:消息。 See the NSControl class specification for more information on the text delegate method. 有关文本委托方法的更多信息,请参见NSControl类规范。

So to achieve your goal, you would implement this delegate, it will get called each time you edit the text inside of the NSTextField .Then inside of that delegate block you will provide the logic needed. 因此,要实现您的目标,您将实现此委托,每次在NSTextField编辑文本时都会调用该委托,然后在该委托块中提供所需的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM