Question

I'm trying to code a multiple panel preference window with an NSToolbar as seen in a lot of Mac applications. There are a few tutorials that do this by programmatically populating the toolbar but that seems to require a lot of "mechanical" code that I thought to save by setting up the whole toolbar in IB. Each NSToolbar item calls the action below and the tag is used to select the view (referenced from the same nib with an IBOutlet):

-(IBAction)changeViewController:(id) sender {
    NSToolbarItem *tbi = (NSToolbarItem*)sender;
    NSString* label = [tbi label];
    NSInteger tag = [tbi tag];
    [self displayViewController:tag];
    NSWindow* window = [self window];
    [window setTitle:label];
}

Then I set Autovalidates on each NSToolbarItem which will give the selected/deselected look on the toolbar items.

Now here is my problem: I was not able to figure out a good way to set the initial state. The moment I open the Window none of the toolbar items is selected and I do not get any udpate to the code that would allow me to select this correctly either. What I came up with is this:

- (void) awakeFromNib { 
    [self displayViewController:kGeneralPrefsTag];
    NSWindow* window = [self window];
    NSToolbar *toolbar = [window toolbar];
    [toolbar setSelectedItemIdentifier:@"generalPreferences"];
    [window setTitle:@"General"];
}

This is all redundant information: 1) I need to declare the Item Identifiers for the toolbar item just for this 2) I manually duplicate the name of the 1st preference panel to be able to set the window title.

Is there a better way to initialize the toolbar ? How could I call the first item of the toolbar so that it will invoke the changeViewControllerAction ?

thanks.

Was it helpful?

Solution

I'm using the toolbar to select different tabs and used the following:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [item1 setEnabled: YES];
    [toolbar setSelectedItemIdentifier: @"needItToolbarItem"];

    [item2 setEnabled: YES];
    [item3 setEnabled: YES];
    [item4 setEnabled: YES];
}

This seemed to get me what I wanted without too much verbiage. I'm not sure if this is the answer you are looking for, but it seems like you were one the right track.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top