Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
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.

Thanks,

MadDoc,
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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?

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.
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
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?

Thanks,

MadDoc
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
On a related note - how do you get a search field in the toolbar (like Safari)?

MadDoc
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
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

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.

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

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.
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
@Gelfin:

: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!).

MadDoc,
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.