如何在Swift中禁用macOS X的NSToolbar按钮?

时间:2021-02-14 07:33:40

I would like to enable and disable programmatically a toolbar item (a button) of a NSToolBar in macOS. I tried to create an outlet to ViewController control-dragging but Xcode don't accept the drop. How can I do this?

我想以编程方式启用和禁用macOS中NSToolBar的工具栏项(按钮)。我试图为ViewController拖拽创建一个outlet,但Xcode不接受drop。我该怎么做呢?

I read this question about a similar question with Objective-C answers but I'm a beginner and can't understand well how to do this in Swift.

我读过一个类似的问题,关于Objective-C的答案,但是我是一个初学者,我不能很好地理解怎么用Swift写。

1 个解决方案

#1


1  

Implement the delegate method

实现委托方法

override func validateToolbarItem(_ item: NSToolbarItem) -> Bool

You can distinguish the items for example by the itemIdentifier and return true to enable and false to disable the item

例如,您可以通过itemIdentifier来区分项目,并返回true to enable和false来禁用项目

The documentation provides an example.

该文档提供了一个示例。

#1


1  

Implement the delegate method

实现委托方法

override func validateToolbarItem(_ item: NSToolbarItem) -> Bool

You can distinguish the items for example by the itemIdentifier and return true to enable and false to disable the item

例如,您可以通过itemIdentifier来区分项目,并返回true to enable和false来禁用项目

The documentation provides an example.

该文档提供了一个示例。