How to make action to close NSPopover? - cocoa

Hi I implemented an NSPopover in one of my apps, but the popover will not close. I'm guessing that it should close once I click something else, but its not. I know that there's a close popover action but that only works for a different button. How can I make it look to see that the popover is open and when it is open to close the popover when I press the same button?

I solved the problem!
First go ahead and click your .xib file.
Where you see the first responder box and files owner - click on the popover.
On the sidebar go to the attributes center
Change the behavior from Application-Defined to Transient and it should work!
That should make the popover disappear once someone has clicked outside the popover.
Another way to close the popover is to have a separate button.
Just add another NSButton and then link it to the popover on the sidebar and set it to Received actions -> perform close:

Related

How do you make a button automatically press without user interaction in Swift Cocoa

This sounds like a weird question, but I am using Swift 2.0 and Xcode to create a cocoa app, and I have a popover linked to an invisible button. I wan't to activate it so it pops up after the user has been on the page for a little while. I have a timer set, but I can't figure out the code to get the button to actually activate at that time, so that the popover will show. I am using a button so I can have an access spot for the popover to show at.
Right now, if you click the (invisible) button, everything shows perfectly. However, I need the click to activate programmatically. I would really appreciate any help!
Thanks!

Swift - NSMenu closes when clicking outside of menu

so i have a simple NSMenu. It is attached to a Status Item Button.
When i click the Button it pops Open.
Now my Problem is that as soon as i click outside of the NSMenu, it closes. How can i avoid that ?
I did try many things but cant get that to work.
The reason i need that is that when the user clicks something in the NSMenu, another window opens, and while this window is open i also want the NSMenu to stay open. But it just does not work.
I feel like its impossible.
Many Thanks in advance.
I believe you have to create a custom view that pops up but something that could be done natively with just NSMenu.

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.

NSStatusItem app focus

How can my menubar application achieve the same behaviours as 1Password or Dropbox:
clicking their menu bar icons or popovers does not steal focus, e.g. while I am in for example TextEdit and open 1Password/Dropbox, the blinking cursor disappears but the window itself does not go into the background, yet I can type into 1Password's text field.
even though they didn't take focus in the first place, they disappear when I click back into another application
I figured out how mouse over works in the Dropbox popover table view by using an NSTrackingArea with the options MouseEnteredAndExited, AssumeInside, and ActiveAlways.
I am trying to get the same behaviour to work in an NSPopover that opens from an NSStatusItem.
I found a workaround for now. I am able to get the same behaviour by using non-activating NSPanel with a window level kCGPopUpMenuWindowLevelKey and I had to override canBecomeKeyWindow to return true.
Unfortunately I haven't found a way yet to get a NSPopover to behave this way since it's not a subclass of NSWindow.
To set the kind of behavior you are describing you use:
yourPopover.behavior = .transient

bindings in Interface Builder

I want to have a button where if I click it, then some text is displayed that says "you clicked the button." I know how to do this using IBOutlet and IBAction. Can it be done with bindings? If so, then how?
I don't think so. You can't bind a momentary button to a property and expect that to work correctly. What would you bind on the button side? After pressing and releasing a momentary button, the button's state is unchanged, so there's nothing to bind do.

Resources