objective-cmacoscocoarubymotion

NSTokenField click completion list item


I have an NSTokenField in my application and I implemented the tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: method in which I specify the receiver’s tokenizing character set with the setTokenizingCharacterSet: method:

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

It works as expected when I click on the space bar or the enter button. I would also like to have the same behavior when I click on one of the item of the completion list with my mouse.

How is that possible?

Thanks for your help.


Solution

  • I don't know if it's possible to have this behaviour with a NSTokenField.

    But you should take a look at MTTokenField which do what you want out of the box.

    To do so, you will have to:

    1.Create an Xcode Project as a Static Library(Do not enable ARC).

    2.Save your project to vendor/MTTokenField

    3.Drag and drop all the files of MTTokenField located in the subdirectory 'MTTokenField' to your new XCode project. Choose to copy the files.

    4.Add this to your rakefile in order to compile and link the library with your Rubymotion project.

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

    5.In Interface Builder change the class of your NSTokenField to NSTextField and then set its custom class to MTTokenField, and also change the custom class of the cell: MTTokenFieldCell instead of NSTextFieldCell.

    6.Then you have to set the delegate of your MTTokenField to a class which has to respond to :

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

    And that's it. It should work.

    Hope it helps !