监听我的文本字段的值变化

发布于 2024-11-10 13:41:30 字数 271 浏览 2 评论 0原文

我试图了解如何从窗口中的文本字段捕获“文本更改”事件。我习惯了 Java 的“动作监听器”,在 Objective-C/Cocoa 中找不到类似的东西。 我搜索了很长一段时间,找到了“键值观察”协议,但是 observeValueForKeyPath: 方法(函数?)仅在代码中更改我的文本字段的值时才会触发(使用 [textfield setStringValue:...],例如),而不是通过键入它。

当用户在文本字段中键入内容时,如何“监听”值的变化?

I'm trying to understand how to catch a "text changed" event from a text field in my window. I'm used to Java's "action listeners", and can't find anything similar in Objective-C/Cocoa.
I searched for quite a while and found the "key value observing" protocol, but the observeValueForKeyPath: method (function?) only triggers when the value of my text field was changed in code (using [textfield setStringValue:...], e.g.), not by typing in it.

How can I "listen" to the value change when a user types in the text field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小伙你站住 2024-11-17 13:41:30

您可以为 NSTextField 实例设置委托,并让该委托实现以下方法:

- (void)controlTextDidChange:(NSNotification *)notification {
    // there was a text change in some control
}

您的委托对象可以是应用程序委托、窗口控制器、视图控制器或应用程序中的其他对象。可以通过编程方式设置委托

[myTextField setDelegate:delegateObject];

,或者在 Interface Builder 中通过 NSTextField 控件中提供的 delegate 出口进行设置。

请注意,如果有多个控件挂接到同一委托,则将为每个控件发送 -controlTextDidChange:,即为不同的控件调用相同的方法。如果您希望根据文本发生更改的控件采取不同的行为,可以使用 -[NSNotification object] 来标识发送通知的控件。

例如,如果您有两个带有相应插座 nameFieldaddressField 的文本字段,并且您为这两个字段设置了相同的委托,那么:

- (void)controlTextDidChange:(NSNotification *)notification {
    // there was a text change in some control
    // [notification object] points to the control that has sent
    // the notification

    if ([notification object] == nameField) {
        // nameField has changed
    }
    else if ([notification object] == addressField) {
        // addressField has changed
    }
}

或者,您可以有一个每个文本字段的委托。在这种情况下,无需测试[通知对象]

You can set a delegate for your NSTextField instance and have the delegate implement the following method:

- (void)controlTextDidChange:(NSNotification *)notification {
    // there was a text change in some control
}

Your delegate object can be the application delegate, a window controller, a view controller, or some other object in your application. The delegate can be programatically set via

[myTextField setDelegate:delegateObject];

or, in Interface Builder, via the delegate outlet available in the NSTextField control.

Note that if there are multiple controls hooked to the same delegate then -controlTextDidChange: will be sent for each control, i.e., the same method is called for different controls. If you want different behaviour according to the control where the text has changed, you can use -[NSNotification object] to identify the control that has sent the notification.

For instance, if you have two text fields with corresponding outlets nameField and addressField, and you’ve set the same delegate for both fields, then:

- (void)controlTextDidChange:(NSNotification *)notification {
    // there was a text change in some control
    // [notification object] points to the control that has sent
    // the notification

    if ([notification object] == nameField) {
        // nameField has changed
    }
    else if ([notification object] == addressField) {
        // addressField has changed
    }
}

Alternatively, you could have one delegate for each text field. In this case, there’d be no need to test [notification object].

憧憬巴黎街头的黎明 2024-11-17 13:41:30

您还可以从 IB 连接到“编辑已更改”并创建操作来处理它

- (IBAction)txtField_Changed:(id)sender
{
   // my textfield has been changed
}

You can also just hook up to the "Editing Changed" from IB and create the Action to handle it

- (IBAction)txtField_Changed:(id)sender
{
   // my textfield has been changed
}
挽容 2024-11-17 13:41:30

这对我有用

func textView(textView: NSTextView, shouldChangeTextInRange affectedCharRange: NSRange, replacementString: String?) -> Bool {
    print("Changed!")
    return true
}

This works for me

func textView(textView: NSTextView, shouldChangeTextInRange affectedCharRange: NSRange, replacementString: String?) -> Bool {
    print("Changed!")
    return true
}
习ぎ惯性依靠 2024-11-17 13:41:30

您可以使用UITextFieldDelegatetextFieldShouldBeginEditing:方法。在 iOS 中,侦听器称为 NSNotifications

编辑

在 Objective-C 中,很多 UIObject 都有一个相应的协议类,称为“委托”,委托负责对事件做出反应。因此,为了能够响应或收到有关操作的通知,您需要实现委托及其方法。

You can use textFieldShouldBeginEditing: method of UITextFieldDelegate. In iOS listeners are called NSNotifications

EDIT

In objective-c a lot of UIObjects have a corresponding protocol class that's called "delegate" The delegate is responsible for reacting to events. So to be able to respond or to be notified about actions you need to implement the delegate and its methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文