views:

44

answers:

2

I need to programatically remove a button from a window in Cocoa but the only method close to this was dealloc in NSObject, but this is not what I want to do. Is there a way to actually delete the button from the window not just deallocate its memory?

+7  A: 

Send the removeFromSuperview message to the button instance.

Though perhaps you just want to hide it instead (using setHidden:)?

Ciarán Walsh
Worth pointing out that views retain their subviews, which means that removing the button from its superview will cause the superview to release it, possibly resulting in it deallocking. I agree that hiding the button is probably more useful.
Peter Hosey
+4  A: 

An NSButton is a subclass of NSControl, which itself is a subclass of NSView.

You should be able to remove the button from it's superview by calling -removeFromSuperView on the button instance.

Jasarien