3

我正在使用最新的 XCode 6.4 和 Swift 1.2。我正在尝试以编程方式在我的 NSToolbar 中选择一个 NSToolbarItem。我在互联网上搜索了很多,应该有 NSToolbar 的方法,称为setSelectedItemIdentifier,但该方法不存在(不再存在?)。

还有另一种方法可以做到这一点吗?也许是这样的:

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

但我不知道我应该用什么来代替“点击”。提前致谢!

Neo 回复后更新: 我希望工具栏项在视觉上被“选中”,从而改变其颜色。我知道如何调用函数,但这不会改变工具栏项的“选定”状态。因此,如果我可以模拟鼠标单击 nstoolbaritem,那将解决我猜的问题(或替换 NSToolbar.setSelectedItemIdentifier)。

4

2 回答 2

1

selectedItemIdentifier现在是 NSToolbar 上的可设置属性,可以用来代替setSelectedItemIdentifier. 确保 NSToolbarItem 是可选的(如果使用 Interface Builder,默认情况下禁用),并且您已经为工具栏项设置了标识符(或使用 NSToolbarItem 的 itemIdentifier)。

然后,只需将 设置selectedItemIdentifier为有效的工具栏项标识符。如果在没有情节提要的情况下实施:

//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"
于 2017-02-28T08:11:32.817 回答
0

好吧,您的 ToolbarItem 与 ViewController 中的 func 连接...您唯一需要做的就是像调用任何其他 func 一样调用该 func,或者有什么特别的吗?

你能给我更多的代码吗?

于 2015-08-03T17:31:09.410 回答