PDA

View Full Version : NSToolbar/GenericToolBar: How do you enable or disable a NSToolBarItem?




LoveMyMac2004
Aug 27, 2008, 06:00 PM
Hi all,

I am using XCode/IB 2.5. I am trying to create an app that has a toolbar. What I would like to do is have the toolBarItem be enabled when it is available/can be used and disabled when it is not. I have downloaded GenericToolBar and it is quite easy to set up and use. NSToolBar is more not as easy to set up (and since I am still learning Cocoa/OBJ-C I am going with ease of setup and understanding). If the ToolBarItem is enabled when I set up the toolBar, then when I try to disable it like this:

[myToolBarItem setEnabled:![myToolBarItem isEnabled]];

it compiles, and executes fine. However the toolBarItem does not change state. Likewise if I disable the toolBarItem, setEnabled:YES does not change its state. If I check the state by using NSLog, I can see that it is changing. Also if I go to customize the toolBar, the items in the list reflect their current state. I can remove the item then add it back in and it changes that way. Not the way I would expect it to work.

Is there some other piece of the puzzle I am not aware of?

Any help is greatly appreciated.

Thanks in advance,

-Don



kainjow
Aug 27, 2008, 06:53 PM
You need to implement validateToolbarItem:

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSToolbarItemValidation_Protocol/Reference/Reference.html

LoveMyMac2004
Aug 27, 2008, 07:41 PM
Thank you so much.

Now my next question is this: Can I just return the enable status of the item? Something like this:


-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem
{
return [toolbarItem isEnabled];
}


And it will validate my toolbarItem based upon it's isEnabled?

Perhaps I am asking a silly question. What would you use this method for? It seems totally unnecessary to me. But I am just learning. :D

Thanks again

-Don

lucasgladding
Aug 27, 2008, 09:27 PM
The Toolbar Programming Topics document contains quite a bit of useful information, so it is probably worth reading even if you are new to Cocoa. You don't need to understand or remember everything you read, but you can often pick up enough to get started. Most delegates in Cocoa are similar, so this time would be well spent.

The Developer/Examples/AppKit/SimpleToolbar source code has enough of the delegate implemented that you can cut and paste most then tweak the code for your needs. IMHO, never choose something like GenericToolbar simply because you don't understand the standard NSToolbar delegate. You will need to learn about implementing delegates at some point, and you don't want to spend time debugging your application only to discover that someone else's code is at fault.

When starting Cocoa development, I largely ignored things like notifications, categories, and protocols. I suspect my motivations were very similar to yours. After rewriting much of that original code, I now realize sometimes it is better to put in the time up front ;) .

Sorry, I completely avoided your last question until now. I'm not positive about this, but I imagine the toolbar uses the validation methods to set enabled or disabled as one of the last steps in displaying itself. Try moving whatever logic you have already written for the setEnabled: method to the validateToolbarItem: method. Bottom line: Cocoa classes are usually doing 5x more work than you think they are.

Best of luck

Luke Gladding