How to disable a button of a NSToolbar of macOS X in Swift?
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
The documentation provides an example.
how to enable/disable NSToolbarItem
Implement NSToolbarItemValidation Protocol in your window, view or document controller. The documentation gives the following sample code:
-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem {
BOOL enable = NO;
if ([[toolbarItem itemIdentifier] isEqual:SaveDocToolbarItemIdentifier]) {
// We will return YES (enable the save item)
// only when the document is dirty and needs saving
enable = [self isDocumentEdited];
} else if ([[toolbarItem itemIdentifier] isEqual:NSToolbarPrintItemIdentifier]) {
// always enable print for this window
enable = YES;
}
return enable;
}
You can also use action
or tag
to determine what toolbar item is being validated. Items are validated frequently, whenever your app is activated or events are dispatched, so they will always be in a valid state.
How can I hide an NSToolbarItem using code?
If disabling them is not enough you could remove the items.
[myToolbar removeItemAtIndex:itemIndex];
Then when you need to put them back, just re-insert them:
[myToolbar insertItemWithItemIdentifier:itemIdentifier atIndex:itemIndex];
Look at Adding and Removing Toolbar Items in the Apple docs.
How to implement validateToolbarItem(_:)?
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)
}
When the application launches, create an array of toolbarItemState. If you want to change the state of the undo toolbar item to 'on,' for example,
editToolItem(index: toolItems.savePict.undo, state: "1")
. Now, the undo toolbar item is one. If you set the state to "0," the button will be disabled.
NSToolbar in Xcode 7 using Storyboards (NSWindowController - NSSplitViewController)
OK, I've created this question quite a few days ago and no answer so far so I've answer with what I recently did to overcome the problem.
After I created my Xcode project I did this:
- Created a subclass MySplitViewController for the NSSplitViewController
Added an IBOutlet for each NSSplitViewItem. For example:
@IBOutlet weak var mySplitViewItem: NSSplitViewItem!
Created a subclass WindowController for the NSWindowController
- Added an IBAction in the WindowController class that links to the NSToolbarItem (my button)
Added a property that gets the Window Controller's content as MySplitViewController
var mySplitViewController: MySplitViewController {
return self.window?.contentViewController as! MySplitViewController
}Now I can access the split view controller's property from the Window Controller in the action I created:
mySplitViewController. mySplitViewItem.collapsed = true
I created some sample code that does this (but using a view controller and changing the text for a label here, just in case someone wants to see a working project with this behaviour. And a blog post about it too :)
Related Topics
Using Nsdate to Get Date for Easter
How to Get The Nearest Int Floored from a Sqrt of an Int
Idiomatic Way to Test Swift Optionals
Sdwebimage, Swift: Sdwebimagerefreshcached Unresolved Identifier
Changing a Label in Prepareforsegue
List Inside Scrollview Is Not Displayed on Watchos
Onreceive String.Publisher Lead to Infinite Loop
How to Access Results from a Realm in Swift
F# Asynchronous Http Request - Parse JSON Response
Print Not Working in Swift 3 Extensions
Rxswift + Mvvm + Coordinator Pattern, How to Wait for Coordinator Result Sequentially
Animate UIlabel Width with Fixed Center
How to Set a Known Position and Orientation as a Starting Point of Arkit
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid
How to Stop Firestore Pagination When Searched Data Are Loaded in Tableview