【问题标题】:NSTokenField click completion list itemNSTokenField 点击完成列表项
【发布时间】:2013-06-21 08:32:23
【问题描述】:

我的应用程序中有一个NSTokenField,我实现了tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: 方法,在该方法中我使用setTokenizingCharacterSet: 方法指定接收者的标记字符集:

  def tokenField(tokenField, completionsForSubstring:substring, indexOfToken:tokenIndex, indexOfSelectedItem:selectedIndex)
    tokenField.setTokenizingCharacterSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
  end

当我单击空格键或回车按钮时,它按预期工作。当我用鼠标单击完成列表中的一项时,我也希望有相同的行为。

这怎么可能?

感谢您的帮助。

【问题讨论】:

    标签: objective-c macos cocoa rubymotion


    【解决方案1】:

    我不知道 NSTokenField 是否可以有这种行为。

    但是你应该看看MTTokenField,它可以做你想做的事情。

    为此,您必须:

    1.将 Xcode 项目创建为静态库(不要启用 ARC)。

    2.将你的项目保存到vendor/MTTokenField

    3. 将位于子目录“MTTokenField”中的所有 MTTokenField 文件拖放到新的 XCode 项目中。选择复制文件。

    4.将其添加到您的 rakefile 中,以便编译该库并将其与您的 Rubymotion 项目链接。

    app.vendor_project("vendor/MTTokenField/", :xcode, :xcodeproj => "MTTokenField.xcodeproj", :target => "MTTokenField", :products => ["libMTTokenField.a"], :headers_dir => "MTTokenField")
    

    5.在Interface Builder中,将你的NSTokenField的类改为NSTextField,然后将其自定义类设置为MTTokenField,同时将单元格的自定义类改为:MTTokenFieldCell而不是NSTextFieldCell。

    6.然后您必须将 MTTokenField 的委托设置为必须响应的类:

    def tokenField(tokenField, completionsForSubstring: substring )
       # your have to return an array containing your results matching substring.
    end
    

    就是这样。它应该可以工作。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      我找到了另一个使用NSTokenField 而不是MTTokenField 的解决方案。

      在我的 NSTokenField 的委托中,我使用了 NSControl 的 controlTextDidChange 方法,只要我在我的令牌字段中写入一个字符,就会调用该方法。在这个方法中,我检查是否有触发的 NSLeftMouseUp 事件,如果是这样,我模拟点击返回。就是这样。

      def controlTextDidChange(aNotification)
        application = NSApplication.sharedApplication
        event = application.currentEvent
        if event.type == NSLeftMouseUp
          e1 = CGEventCreateKeyboardEvent(nil, 0x24, true)
          CGEventPost(KCGSessionEventTap, e1)
        end
      end
      

      要使其正常工作,还有一件事要做:这里的问题是,如果我有一个包含 3 个项目的完成列表,默认情况下会选择其中一个,比如说第一个。在这种情况下,如果我单击第二项或第三项,解决方案将按预期工作,但我必须双击第一项才能使其工作。

      要解决此问题,请关闭自动补全并仅显示建议框,即将此行添加到 tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: 方法:

      selectedIndex[0] = -1
      

      【讨论】:

      • 知道如何在 Swift 中做到这一点吗?
      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 2014-08-20
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多