【问题标题】:Updating a label as a user is typing Using Cocoa-Applescript在用户输入时更新标签使用 Cocoa-Applescript
【发布时间】:2013-11-08 02:12:56
【问题描述】:

一段时间以来,我一直在寻找这个问题的答案。我已经看到了如何在 Cocoa-Applescript 环境之外执行此操作的示例,但我没有足够的知识将其适应 Applescript 版本。

当用户在文本字段中键入内容时,我正在尝试更新 UI 上的标签。我的应用程序是一个使用 Xcode 5.0 的基于 Cocoa-Applescript 的应用程序(这是作为 Mac 应用程序开发的,而不是 iOS)。

我试过了:

on controlTextDidChange_(aNotification) -- a text field changed, so check it out
    set thisTextField to aNotification's object() -- the current control being changed
    set theText to thisTextField's stringValue()
    -- whatever
end controlTextDidChange_

但这似乎对我不起作用,它只会在用户按下 Enter 或将焦点更改到另一个元素后更新 - 我需要在用户输入时更新它。我还看到了一个在 iOS 应用程序中执行此操作的示例:

[textField addTarget:self action:@selector(textViewDidChange) forControlEvents:UIControlEventEditingChanged];

但我不知道如何将它改编成 Applescript!

任何帮助都会很棒!

【问题讨论】:

  • 你找到解决方案了吗?

标签: objective-c xcode macos cocoa applescript


【解决方案1】:

您可以使用 controlTextDidChange 来完成此操作。您只需将 NSTextField(s) 的委托设置为 Interface Builder 中的 App Delegate 对象,然后将类似内容添加到您的 AppDelegate.applescript 文件中:

property aTextField : missing value
property aTextField2 : missing value

property aTextLabel : missing value


    on controlTextDidChange_(aNotification)

       if aNotification's object() is aTextField then
           set newText to aTextField's stringValue()
           aTextLabel's setStringValue_(newText)
       end if


       if aNotification's object() is aTextField2 then
           set newText to aTextField2's stringValue()
           aTextLabel's setStringValue_(newText)
       end if

    end controlTextDidChange_

【讨论】:

    【解决方案2】:

    通常您不需要任何特殊代码来执行此操作。您可以在 xib 中正确执行此操作,并在代码中使用方法。因此,在您的代码中创建一个方法来接收文本字段的操作。接下来单击 xib 文件并选择窗口中的文本字段。在检查器的连接部分中,将发送的操作选择器挂钩到您创建的方法。现在转到检查器中的属性部分,在操作下拉菜单下,您有 2 个选择; 1) 仅在输入时发送或 2) 在结束编辑时发送。听起来您选择了第一选择。尝试第二个选项,您选择的方法应该会在用户输入时被调用。

    【讨论】:

    • 感谢您的信息。我为此选择了“在结束编辑时发送”。但是我需要在用户在字段中输入时每次按下键时执行该方法 - 而不仅仅是在他们完成时。
    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2013-12-03
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多