View Full Version : A simple tut on NSToolbars?
Jan 21, 2007, 12:48 AM
I have been searching the web, and the only half decent tutorials are the ones on MacDevCenter and CocoaDev (or something like that). I didn't understand them well at all... Does anybody know of a tutorial for total n00bs at cocoa? BTW: i have found a thing called SimpleToolbar, but i don't understand how to add it to my project and customize the buttons. Anybody want to give me a quick, but meaningful (to a n00b) rundown?
Jan 21, 2007, 04:54 AM
I used a really old sample on Apple's website to build a class that loads the definition from an xml file and builds the toolbar for you. Other than that you have to do the entire thing by hand. This is not something you can do in IB.
Edit to add:
This was the sample code (http://developer.apple.com/samplecode/ToolbarSample/index.html)
I can post my code if you want.
Jan 21, 2007, 08:43 AM
Tom, Honestly, buy Programming in Objective-C by Kochan, read that, then read your Cocoa book, Cocoa Programming for Mac OS X by Hillegass. How do you expect to get any where if you mooch off other people?
Jan 21, 2007, 09:12 AM
Apple's code in link provided by robbieduncan seems rather overly complicated for such a relatively simple thing as toolbar. Heres some sample code of the method that I've used in some of my own applications.
// General preferences
NSToolbarItem *general = [[NSToolbarItem alloc] initWithItemIdentifier:@"General"];
[general setImage:[NSImage imageNamed:@"GeneralPreferences"]];
Basically you need to create the above block of code for each of your toolbar items, obviously changing the appropriate fields. Once you've created each of the items you then need to create an instance of NSToolbar to attach to your window. This can be done using:
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"preferenceToolbar"] autorelease];
[[self window] setToolbar:toolbar];
Once you've created all the above code I usually put it all in one method, for example - (void)setupToolbar and then call it from your awakeFromNib method so it dispalys when the window appears on screen.
Remember you must implement the toolbars delegate methods in the delegate you specified above. The methods are:
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
The last 3 methods just return an array of your toolbar item identifiers.
It looks more complicated than it actually is but if you keep researching it you'll eventually get it.
Jan 21, 2007, 07:23 PM
thanks for your help. I can understand (pretty well) what you said stu02. BTW: I can't afford that book. It's $80 in Oz, and i am pretty broke, apart from the money i am saving for iphone. I will try and hassle my dad to get it for me though (keyword: try)