RE: NSToolbarItem not displaying
RE: NSToolbarItem not displaying
- Subject: RE: NSToolbarItem not displaying
- From: Lee Ann Rucker <email@hidden>
- Date: Fri, 18 Feb 2011 00:31:14 -0800
- Acceptlanguage: en-US
- Thread-topic: NSToolbarItem not displaying
> NSLog(@"I am adding %@", [element objectForKey:@"itemIdentifier"]);
Is not adding anything. You're looping through your dictionary, creating items, assigning them to a local variable, and then replacing them with a new item next time through the loop. Then you exit the loop and return the last one created. Also you're leaking them, because they aren't autoreleased.
What I'd do (because while nibs are lovely, sometimes you do have to dynamically generate stuff; there are things you might not know at nib-loading time) is
Rename the "toolbarItems" variable to something like "toolbarElements", because it doesn't contain NSToolbarItems.
Make it a dictionary, keyed off "itemIdentifier".
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
return [toolbarElements allKeys];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag {
NSToolbarItem *toolbarItem;
NSDictionary *element = [toolbarElements objectForKey:itemIdentifier];
toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier:[element objectForKey:@"itemIdentifier"]] autorelease];
... the rest of what was in the loop ....
return toolbarItem;
}
But I'd only do that if this really was something that couldn't be determined at nib-loading time, and even then I'd probably just move the code that generates the elements dictionaries into the above method. Toolbars tend to be fairly non-variable, and this seems like a design you'd use when the content is highly variable.
________________________________________
From: cocoa-dev-bounces+lrucker=email@hidden [cocoa-dev-bounces+lrucker=email@hidden] On Behalf Of Kevin Walzer [email@hidden]
Sent: Thursday, February 17, 2011 7:51 AM
To: email@hidden
Subject: NSToolbarItem not displaying
Hi,
I'm trying to create an NSToolbar programatically, and am having some
problems with all of my NSToolbarItems displaying (all basic items, no
custom views). Specifically, rather than both items in my test case
displaying correctly, the second item is displaying twice.
What I'm doing is setting up the attributes for each NSToolbarItem
(identifier, label, selector, etc.) in an NSMutableDictionary, adding
each dictionary to an NSMutableArray (toolbarItems), adding those items
to another NSMutableArray to set the toolbar's default items
(defaultItems) and allowed items, and then iterating through the
defaultItems array to insert the toolbar item. (Use of NSLog statements
shows that all intended items are being correctly added to the various
arrays.)
Here's my code to set up the default items in the toolbar:
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
NSMutableArray *defaultItems = [[NSMutableArray alloc] init];
for (NSDictionary *element in toolbarItems) {
[defaultItems addObject: [element objectForKey:@"itemIdentifier"]];
}
return defaultItems;
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag {
NSToolbarItem *toolbarItem;
for (NSDictionary *element in toolbarItems) {
toolbarItem = [[NSToolbarItem alloc]
initWithItemIdentifier:[element objectForKey:@"itemIdentifier"]];
[toolbarItem setLabel:[element objectForKey:@"buttonLabel"]];
[toolbarItem setToolTip:[element objectForKey:@"toolTip"]];
[toolbarItem setImage:[[NSImage alloc]
initWithContentsOfFile:[element objectForKey:@"imagePath"]]];
[toolbarItem setAction:@selector(runScript:)];
[toolbarItem setTarget:self];
NSLog(@"I am adding %@", [element objectForKey:@"itemIdentifier"]);
[toolbarItem setEnabled:YES];
NSLog(@"the label is %@", [toolbarItem label]);
}
NSLog(@"the final label is %@",[toolbarItem label]);
return toolbarItem;
}
When the code iterates through the NSToolbarItems, the correct
itemIdentifier is logged, i.e. "I am adding item1," "I am adding item2,"
and so on. However, after the loop, the "final label" is always logged
as the label for "item2." item1 simply disappears; item2 is displayed
where item1 should be, and item2 is correctly displayed in its own
intended spot. This is where item1 is disappearing, since the log
statement show that it is correctly retrieved from the array.
Any suggestions as to what I'm doing wrong here?
--Kevin
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden