Question

I'm adding a toolbar programmatically inside an interface inheriting NSObject <NSToolbarDelegate>, and implementing these methods:

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)willBeInsertedIntoToolbar;
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar 
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar

I also add a button by calling setView on a NSToolbarItem. This view contains an NSButton and is in the .XIB interface.

However, setAction on the same item does not work, due to reason described at http://www.cocoabuilder.com/archive/cocoa/291782-nstoolbaritem-custom-view-setaction.html#291783.

How do I implement this solution?

Was it helpful?

Solution

You could set the target and action of the NSButton in the nib file itself, or if you need to do it programmatically, then create an IBOutlet to the NSButton and do it in code.

When you use an NSButton in a toolbar item, it effectively acts like an NSButton would anywhere else in your interface, rather than as an NSToolbarItem per se. For example, you won't be able to easily disable or enable the button through the use of the standard -validateToolbarItem: or -validateUserInterfaceItem:; rather, you'll need to have an IBOutlet to the button in question, or otherwise use bindings to enable or disable the button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top