How to programmatically select an NSToolbarItem on an NSToolbar using Swift 1.2 / Cocoa

| | August 4, 2015

I’m using the latest XCode 6.4, with Swift 1.2.
I’m trying to programmatically select a NSToolbarItem in my NSToolbar. I’ve searched a lot on the internet and there should be method for the NSToolbar called setSelectedItemIdentifier, but that method doesn’t exist (anymore?).

Is there another way to accomplish this?
Perhaps something like:

NSApplication.sharedApplication().sendAction(Selector("click"), to: myNSToolbarItem, from: sender)

But I don’t know what I should use instead of “click”.
Thanks in advance!

Update after Neo’s reply:
I want the toolbaritem to become visually “selected”, which changes its colour. I know how to call the func, but that won’t change the “selected”-state of the toolbaritem. So, if I could simulate a mouse click on the nstoolbaritem, that would solve my problem I guess (or a replacement of NSToolbar.setSelectedItemIdentifier).

One Response to “How to programmatically select an NSToolbarItem on an NSToolbar using Swift 1.2 / Cocoa”

  1. Well your ToolbarItem is connected with a func in your ViewController… The only thing you need to do is call that func like you call any other func, or is there something special?

    Could you give me some more code?

Leave a Reply