Re: Releasing Menu Resources?
Re: Releasing Menu Resources?
- Subject: Re: Releasing Menu Resources?
- From: Clark Cox <email@hidden>
- Date: Wed, 9 Jun 2004 10:06:43 -0400
On Jun 09, 2004, at 09:34, Jerry LeVan wrote:
>
I am adding a dynamically generated menu to one of my applications
>
that creates a menu of all of the sql files in a library folder
>
with submenus for the subdirectories. I initially create the
>
"Script" menu in awakeFromNib. ( The result looks like the
>
Script menu in the right hand side of the menu bar.)
>
>
Since users can create/add scripts on the fly I would like to
>
be able to update the menu on demand (or automagically).
>
>
It appears to me that I need to destroy the existing menu
>
structure and then call the build routine to catch any
>
changes.
>
>
One question is: In order to properly release the menu resources
>
do I need to walk the "main" menu and recursively release the
>
submenus and then release the items on the "main" menu?
Normally, no. Just remove the items from the menu, and that takes care
of the release (as long as no one else has retained references to the
items). However, there is one place where you should add a release to
your code to prevent a major leak, as indicated below.
>
>
For what it is worth, here is the build code that I am
>
using in a mini-app before placing in the main application.
>
It is initially called with menu set to the Script menu in
>
the application menubar and thePath set to the path of the
>
script library folder.
>
******************************
>
-(IBAction) buildScriptMenu: (NSMenu*) menu withPath:
>
(NSString*)thePath
>
{
>
BOOL isDir;
>
NSString *dirPath;
>
NSFileManager *manager = [NSFileManager defaultManager];
>
NSArray *dirArray = [manager directoryContentsAtPath:thePath];
>
NSEnumerator *direnum = [dirArray objectEnumerator];
>
NSString *pname;
>
while (pname =(NSString*) [direnum nextObject]) {
>
// Is it a directory?
>
dirPath = [NSString stringWithFormat:@"%@/%@",thePath,pname];
>
if( ([manager fileExistsAtPath:dirPath isDirectory:&isDir] &&
>
isDir))
>
{
>
NSMenuItem *tmp = [menu addItemWithTitle:pname action:nil
>
keyEquivalent:@""];
>
NSMenu *newMenu = [[NSMenu alloc] initWithTitle:pname];
>
[menu setSubmenu:newMenu forItem:tmp];
[newMenu release];
>
[self buildScriptMenu: newMenu withPath:dirPath];
>
}
>
else
>
// if ([[pname pathExtension] isEqualToString:@"sql"])
>
{
>
if ( [pname characterAtIndex:0] == (unichar)'.') continue; //
>
skip dot files
>
NSMenuItem *tmp = [menu addItemWithTitle:pname
>
action:@selector(loadScript:) keyEquivalent:@""];
>
[tmp setRepresentedObject:dirPath];
>
}
>
}
--
Clark S. Cox III
email@hidden
http://homepage.mac.com/clarkcox3/
http://www.livejournal.com/users/clarkcox3/
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.