问题
I want to know how to close NSPopover programmatically, not by touching outside, because I want to assign it to an action (such as KeyDown Enter Key or other shortcut)
because I open my NSPopover with a shortcut, it would be more logical to close by pressing another command
going to share my code:
EdiciondeCuentasWC.h (NSWindowController) from where I call my NSPopover
#import "EdicionDeCuentasWC.h"
#import "CambiarTipoCuentaVC.h"
@interface EdicionDeCuentasWC ()<NSPopoverDelegate>{
CambiarTipoCuentaVC *cambiarTipoCuentaVC;
}
@property (strong) IBOutlet NSPopover *popoverClasifCuentas;
@end
@implementation EdicionDeCuentasWC
-(void)mostrarPopupCambiarTipoCta{
cambiarTipoCuentaVC = (CambiarTipoCuentaVC *) _popoverCambiarTipoCuentas.contentViewController;
cambiarTipoCuentaVC.nombre_tipo_cta = arrayActivos[renglonSeleccionado][@"nombre_tipo_cta"];
cambiarTipoCuentaVC.prioridad_cta = arrayActivos[renglonSeleccionado][@"prioridad_cta"];
NSTableCellView *cellView = [_activoTableView viewAtColumn:0
row:renglonSeleccionado
makeIfNecessary:NO];
[_popoverClasifCuentas setDelegate:self];
[cambiarTipoCuentaVC inicializarDatos];
[_popoverCambiarTipoCuentas showRelativeToRect:[cellView bounds] ofView:cellView preferredEdge:NSMaxXEdge];
}
#pragma mark NSPopoverDelegate
-(void)popoverWillClose:(NSNotification *)notification{
NSPopover *popover = (NSPopover *)[notification object]; //there I have the code for managing all the returning parameters...
}
@end
And the code of my NSPopover is inside of a NSViewController (CambiarTipoCuentaVC) but inside there I there's neither [self close] nor [self performClose] for making it close from a button, or shortcut, any help to make it work I'd appreciate...
回答1:
Have you even looked as far as the NSPopover
documentation? It has a -close
method and, for a slightly different purpose, -performClose:
method.
回答2:
I came across this post and wanted to share my solution.
>macOS 10.10, Swift 4
Since macOS 10.10 you can call
presentViewController(_ viewController: NSViewController, asPopoverRelativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge, behavior: NSPopoverBehavior)
on NSViewController
to present another View Controller as a popover.
When you do that, the presenting View Controller is available via the presenting
property. Thus, you only need to call presenting?.dismissViewController(self)
to dismiss the popover.
I hope that's helpful for anyone looking for a modern solution to this problem.
回答3:
I found how, Im going to complement ken Thomases's Answer
I created a subclass of NSPopover called MyNSPopover
then I added next code:
#import "MyNSPopover.h"
@implementation MyNSPopover
-(void)keyDown:(NSEvent *)theEvent{
//if enter key is pressed, the NSPopup will be closed
if (theEvent.keyCode == 36) {
[self close];
}
}
@end
and then just added this class to my NSPopover like this
done just works
回答4:
What you want to close is the window of your popover, so you can simply add
@IBAction func closePopover(_ sender: Any) {
self.view.window?.performClose(sender)
}
to your popover's view controller, no subclassing needed.
(Xcode 10, macOS 10.13, Swift 4.1)
来源:https://stackoverflow.com/questions/25149896/how-to-close-nspopover-programmatically