A simple tut on NSToolbars?

Discussion in 'Mac Programming' started by tominated, Jan 20, 2007.

  1. tominated macrumors 68000


    Jul 7, 2006
    Queensland, Australia
    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?
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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

    I can post my code if you want.
  3. CaptainZap macrumors regular

    Jan 17, 2007
    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?
  4. stu02 macrumors newbie

    Aug 1, 2006
    Edinburgh, Scotland
    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 setLabel:@"General"];
    [general setImage:[NSImage imageNamed:@"GeneralPreferences"]];
    [general setTarget:self];
    [general setAction:@selector(displayGeneralPreferences:)];
    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];
    [toolbar setDelegate:self];
    [toolbar setSelectedItemIdentifier:@"General"];
    [[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.
  5. tominated thread starter macrumors 68000


    Jul 7, 2006
    Queensland, Australia
    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)

Share This Page