如何实现 validateToolbarItem(_:)?
How to implement validateToolbarItem(_:)?
以下 and the documentation示例。我尝试实现一段代码,在 macOS 工具栏中启用和禁用两个按钮(撤消和重做)。
override func validateToolbarItem(_ toolbarItem: NSToolbarItem) -> Bool {
var enable = false
if toolbarItem.itemIdentifier.isEqual("undoButton") {
enable = (mainTextField.undoManager?.canUndo)!
}
else if toolbarItem.itemIdentifier.isEqual("redoButton") {
enable = (mainTextField.undoManager?.canRedo)!
}
return enable
}
不幸的是,代码似乎没有效果。我错过了什么?
enum toolItems:Int {
case undo = 0
case redo = 1
}
// creating an array at the beginning (AppleDelegate, windowDidLoad, ...) //
func makeToolbar() {
toolbarItemState.insert("1", at: toolItems.undo.rawValue) // 0
toolbarItemState.insert("0", at: toolItems.redo.rawValue) // 1
}
override func validateToolbarItem(_ toolbarItem:NSToolbarItem) -> Bool {
var enable:Bool = false
if ((toolbarItemState[toolbarItem.tag] as AnyObject).integerValue == 1) {
enable = true
}
return enable
}
func editToolItem(index:Int,state:String) -> Void {
toolbarItemState.replaceObject(at: index, with: state)
}
当应用程序启动时,创建一个 toolbarItemState 数组。如果要将撤消工具栏项的状态更改为 'on,' 例如,
editToolItem(index: toolItems.savePict.undo, state: "1")
。现在,撤消工具栏项是其中之一。如果将状态设置为“0”,按钮将被禁用。
以下
override func validateToolbarItem(_ toolbarItem: NSToolbarItem) -> Bool {
var enable = false
if toolbarItem.itemIdentifier.isEqual("undoButton") {
enable = (mainTextField.undoManager?.canUndo)!
}
else if toolbarItem.itemIdentifier.isEqual("redoButton") {
enable = (mainTextField.undoManager?.canRedo)!
}
return enable
}
不幸的是,代码似乎没有效果。我错过了什么?
enum toolItems:Int {
case undo = 0
case redo = 1
}
// creating an array at the beginning (AppleDelegate, windowDidLoad, ...) //
func makeToolbar() {
toolbarItemState.insert("1", at: toolItems.undo.rawValue) // 0
toolbarItemState.insert("0", at: toolItems.redo.rawValue) // 1
}
override func validateToolbarItem(_ toolbarItem:NSToolbarItem) -> Bool {
var enable:Bool = false
if ((toolbarItemState[toolbarItem.tag] as AnyObject).integerValue == 1) {
enable = true
}
return enable
}
func editToolItem(index:Int,state:String) -> Void {
toolbarItemState.replaceObject(at: index, with: state)
}
当应用程序启动时,创建一个 toolbarItemState 数组。如果要将撤消工具栏项的状态更改为 'on,' 例如,
editToolItem(index: toolItems.savePict.undo, state: "1")
。现在,撤消工具栏项是其中之一。如果将状态设置为“0”,按钮将被禁用。