Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable/disable NSToolbarItem

I have a project that needs to disable/enable some NSToolbarItems depends on different options. I checked and found no parameter for this.

Is there a way to enable/disable a given NSToolbarItem?

like image 200
monsabre Avatar asked Nov 05 '11 02:11

monsabre


1 Answers

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.

like image 80
Francis McGrew Avatar answered Nov 15 '22 13:11

Francis McGrew