Re: Removing submenus from a menu
Re: Removing submenus from a menu
- Subject: Re: Removing submenus from a menu
- From: j o a r <email@hidden>
- Date: Mon, 13 Jan 2003 07:19:27 +0100
On Monday, Jan 13, 2003, at 03:08 Europe/Stockholm, Greg Hurrell wrote:
In my case, I wanted to have the menu "stick around" so that I could
add it back in later if necessary with a -insertItem:atIndex call. So
the solution was to send the NSMenuItem a -retain before removing it
to increase its retain count; and when adding it back in send a
-release after the -insertItem:atIndex to reduce its retain count. In
this way it's retain count is more or less stuck at 1 for the life of
the program, which is effectively what I want.
Rather than continously retaining and releasing the menu item, what you
should probably do is something like this:
// In awakeFromNib or init
// The menu item is allocated with a positive retain count.
myMenuItem = [[NSMenuItem alloc]
initWithTitle:action:keyEquivlalent:...];
// Added to the menu, the retain count will increase another step.
[myMenu addItem: myMenuItem];
// In your code
// While removing and adding the retain count will never reach 0 even
if
// you don't keep retaining and releasing it.
remove, add, remove, add...
// When you're all done, in dealloc or otherwise
// When the menu is released it will release all the menu items, but
you still
// need to balance the initial retain from the alloc operation.
[myMenuItem release];
j o a r
_______________________________________________
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.