Tutorials For Toolbars

Hello everyone,
Could someone point me in the direction towards a tutorial on toolbars in ASOC? I am using them but haven’t quite figured them out. Thanks!

I’m not sure there’s much to figure out, so perhaps you should be specific. FWIW, there’s one used in the project with chapter 23 of my book.

Okay, I am still a beginner so all I want to know is how to change my window when I click on a toolbar item.

What do you mean by “change”? Toolbar items are like buttons: you connect them to handlers that are called when they are clicked.

When you click on the toolbar item, it changes the appearance of the window. For example, I have a toolbar and two buttons, when I click on the toolbar the button changes from one position to another.

I’m not sure I follow you. Generally, you set the initial default layout of the buttons, and it’s up to the user to move them if they want, using View → Customize Toolbar… (this is automatic). I’m not sure why you would be moving them around programmatically; that would be frowned upon in the HIG (and hence probably difficult to do).

Oh, I’m sorry, I seriously have confused you. I meant I had two regular buttons and one toolbar button. I basically want two windows and to switch between them when the user clicks on the toolbar button.

OK, so you’re really interested in how to open and close windows. Opening is usually done using orderFront:, makeKeyAndOrderFront:, orderFrontRegardless, makeKeyWindow, or makeMainWindow. Closing is usually done via performClose:, close, or orderOut.

There’s a good example of what you want, using normal buttons, in chapter 11 of my book.

Okay, so I’ll have to make as many windows as I have toolbar items? Also, if you could include an example for the operations to open and close windows, it would be very much appreciated!

I don’t think so – but I’m afraid I still don’t understand what you’re exactly after.

There’s a working example that comes with the book, and looks at most of the options. It’s not really something that can be covered in a simple post here.

That sounds like what you said you wanted to do in one of your previous posts, but you can do whatever you want to do with them – they’re just like any other button in what actions they can perform. More typically though, they change the view in their window rather than opening a new window, or open specialty panels like a font panel or inspector like they do in Numbers. So what kind of other window(s) do you want to open?

Ric

I wish I could change the view, but if it is too advance, I could handle only changing windows. If you could tell me how to change the views through IB AND the code, it would be very nice!

The easiest way to change views is make a new view in IB and use the window’s setContentView: method to switch between views. Once you’ve dragged a toolbar into your window, you need to click on it to expand it and show the Allowed Toolbar Items (sometimes this didn’t work for me. There seems to be a bug that makes the window go blank when I try this some of the time. Clicking on the disclosure triangle for the objects list on the left of the window seemed to fix it). Drag an Image toolbar item into the Allowed Toolbar Items list and give it an image and name in the inspector. Then you drag it down into the Default Toolbar Items (not up into the toolbar itself). When it comes time to connect an action to the toolbar item, you need to connect it to the one in the Allowed Toolbar Items list not in the toolbar itself (or you can connect it in the object list on the left of the IB window).

To make a new view, drag a custom view out of the object library and place it somewhere off to the side of the main window, then make it the same size as the content view of the window (make sure to set its sizing behavior too, so that it will expand with the window if the users changes the window size). Add any controls or other views that you want in your alternate view, and then that’s all there is to do (of course you need to hook up any controls to outlets or actions just like you would in your main view).

In the code, you need to have outlets to the window and both views (the new one you made and the window’s content view) and an action to connect to your new toolbar item. Here is a simple example that toggles between two views:

script AppDelegate
	property parent : class "NSObject"
    property win:missing value -- IBOutlet to the window
    property altView:missing value -- IBOutlet to the custom view
    property mainView:missing value -- IBOutlet to the window's content view
	    
    on switchViews_(sender) -- IBAction connected to a toolbar item
        if win's contentView() is mainView then
            win's setContentView_(altView)
        else
            win's setContentView_(mainView)
        end if
    end
	
end script

You can add more custom views if you want. I hope this helps.

Ric

Hi,

a preferences-like toolbar with different views changing and resizing smoothly is normally realized with an NSWindowController implementing NSAnimationDelegate and NSToolbarDelegate protocols.

You can find an ObjC example here: DBPrefsWindowController

Whoa, sorry. I am still a beginner and that multi-paged ObjC code page was way too much for me… I know that this is a big inconvenience, but if you could include a small one, for me, that would be appreciated!

The DBPrefsWindowController is actually the basic implementation of an animated toolbar.

I’m sure it could be also written in ASOC

The method I sent you works fine – it’s much simpler than what Stefan posted, and I think that would be good enough for someone at your level.

Ric

Thank you very much, I don’t know how but somehow I skipped over your post! Very sorry rdelmar and thank you all!

Everything works except when I press the (X) button on the top of the window, it doesn’t work. Same thing goes for minimize too.

I’m not sure why that would be. They work on my test project that I made.

Ric