1. Welcome to the new MacRumors forums. See our announcement and read our FAQ


Discussion in 'Mac Programming' started by MadDoc, Nov 22, 2007.

  1. macrumors 6502


    I am trying to make a simple app using ObjC/Cocoa, Xcode 3, IB 3 and Core Data.

    I have read Objective-C Programming and Cocoa Programming For Mac OSX (good but out-of-date).

    I would like to add a toolbar to my main window. I have dragged an instance of NSToolbar to the window but I am not sure how to go about adding buttons to it.

    Can anyone give me a brief idea of how I would add, for example, an 'Add' button to the toolbar, assign it an icon and then explain to me how I would launch a new sheet window when the user clicked on the button?

    I'm sorry for asking what (I hope) is a really easy question but I am a recent convert to Cocoa from REALbasic and am struggling with the poor (although extensive) documentation provided by Apple.


  2. Moderator emeritus


    Double-click the toolbar in the window in IB. A sheet will appear that contains the Allowed Toolbar Items. Here is where you drag toolbar items from the Library window. The toolbar items are located under Cocoa > Application > Toolbars. Drag an Image Toolbar Item onto your Allowed Toolbar Items sheet. You can then change its properties (image, label, etc.). You can control-drag it just like a normal button to connect it to an IBAction. With the Allowed Toolbar Items sheet open, you can drag and drop items to and from the actual window toolbar to add, remove and rearrange items.
  3. macrumors 6502


    Sorry for the late-reply - I've been wine tasting all weekend in the Clare Valley :)

    Following your instructions - I removed all the default icons (like Fonts, etc) and dragged a single 'Image Toolbar Item' to the editing sheet. I then set the image to an icon image I dragged into my Xcode project. The button (with the correct image) shows up when the toolbar editing sheet is displayed but doesn't show when I run Cocoa simulator or when I run my app (note, I haven't connected the button to anything).

    Am I missing something really obvious? Is there a 'Show Button' checkbox somewhere?


  4. macrumors 6502


    On a related note - how do you get a search field in the toolbar (like Safari)?

  5. macrumors 68020


    What you're calling a "toolbar editing sheet" is really the customization panel for the toolbar. In that sheet you're just defining all the things that can possibly be in your toolbar, not all of which necessarily will be by default.

    Dragging items from the sheet to the toolbar on your window in IB works almost exactly like customizing a toolbar in a proper application, only what you're doing is defining the default toolbar configuration.

    This is the process whether you expose toolbar customization to your user or not.

    As someone who comes originally from a Windows development background, where things like this are almost always really hard, I've got to say the answer to this question is one of my absolute favorite things about IB: you just drag one there. No, seriously, it just works. You are by no means limited to things in the Toolbar library palette.
  6. macrumors 6502



    :eek: I'm actually blown away. That is the coolest way to add buttons EVER! Thanks for explaining things :) (I've just come from REALbasic and that was a pain in the rear at times!).


Share This Page