开发者

How do I set the init state of a NSToolbar used for a multi panel preference window?

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 t开发者_StackOverflow社区itle.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜