添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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).

selectedItemIdentifier is now a settable property on NSToolbar which can be used in place of setSelectedItemIdentifier. Make sure the NSToolbarItem is selectable (if using Interface Builder, this is disabled by default), and that you've set an identifier for the toolbar item (or use the itemIdentifier of the NSToolbarItem).

Then, simply set the selectedItemIdentifier to be a valid toolbar item identifier. If implementing without Storyboards:

//Create the toolbar item and set an identifier
let demoToolBarItem = NSToolbarItem(itemIdentifier: "account")
//Set the NSToolbarDelegate
preferencesToolbar.delegate = self
//Implement toolbarSelectableItemIdentifiers(NSToolbar)
func toolbarSelectableItemIdentifiers(NSToolbar) -> [String]{
  return ["account"]
//Set the selected item on the toolbar
preferencesToolbar.selectedItemIdentifier = "account"

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?

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.