Programmatically Removing a Button from a Window in Cocoa OSX - cocoa

I need to programmatically 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?

Send the removeFromSuperview message to the button instance.
Though perhaps you just want to hide it instead (using setHidden:)?

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.

Related

How can I make a Mouse Event persist through closing an NSPopover?

I have a NSPopover that opens, and if the user clicks somewhere else in the app, the popover closes.
But the problem is that currently that mouseDown event is consumed during the popover-closing process.
Is it possible to still have that mouseDown event go through to the application, but also close the popover?
I had this same problem, so we changed to using NSPopoverBehaviorSemitransient for the behavior type. It no longer steals the mouseDown: and we just added some extra cases for closing the popover manually.
You can subclass the windows contentViewControllers view object.
I did this in the Storyboard file.
In there, implement the mouseDown() method. In there, you can create a notification which can be received at a point in your project where you need to know about the mouse event.
As the 'root view' captures almost all mouseDown() events, you have to filter them in order to only respond to the notification when the popover is displayed.
Don't forget to call super.mouseDown() at the end of your implementation.

How can I dismiss NSPopover when not active?

I have a menu bar app that displays an NSPopover when the status icon is pressed. The problem is that it will only close if you click again on the status icon.
I want the view to close when I click anywhere outside the popover itself.
I tried changing the popovers attributes Behaviour to Transient but the issue persists.
What is the best way I can achieve this?
Use UIPopoverController instead of NSPopover and Present it in the required ViewController using the below method
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
So if any other view is clicked apart from the popover, it will dismiss automatically.

How a NSWindow knows it is visible?

Is there a ViewDidAppear equivalent method for a NSWindow on cocoa?
What method runs the moment a window is visible?
I know awakeFromNib is triggered but the window is not visible when it fires.
You probably want windowWillLoad and windowDidLoad to perform tasks before the window nib file is loaded or to perform tasks after the window nib file is loaded.
See NSWindowController Class Reference
If you subclass NSWindowController, you can intercept showWindow: messages.

Get notified when mouse is over a particular NSMenuItem

Is there a way to be notified when mouse enters and exit from a particular NSMenuItem without using a custom NSView?
I tried subclassing NSMenuItem and overriding -(BOOL)isHighlighted but it doesn't seem to work.
Have you got any hint?
Why don't you try setting a menu delegate and receiving the menu:willHighlightItem: delegate call?

Having context menu for NSButton created dynamically

I am creating NSButton dynamically in the cocoa application which is fine. However once the application launches, I want to perform right click on the NSButton created or would like to have context menu for the NSBUtton. How to achieve this?
Subclass NSButton and override mousedown or menu for event

Resources