如何以编程方式关闭NSPopover

Jes*_*sus 0 macos nspopover xcode5.1

我想知道如何以编程方式关闭NSPopover,而不是通过触摸外部,因为我想将其分配给一个动作(例如KeyDown Enter Key或其他快捷方式)

因为我用快捷方式打开我的NSPopover,按下另一个命令关闭更合乎逻辑

要分享我的代码:

EdiciondeCuentasWC.h(NSWindowController),我称之为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
Run Code Online (Sandbox Code Playgroud)

我的NSPopover的代码是在一个NSViewController(CambiarTipoCuentaVC)里面,但在那里,我没有[自我关闭]或[self performClose]使它从一个按钮或快捷方式关闭,任何帮助使它工作,我很感激...

Jan*_*ker 15

我遇到过这篇文章并希望分享我的解决方案.

> macOS 10.10,Swift 4

从macOS 10.10开始你可以打电话

presentViewController(_ viewController: NSViewController, asPopoverRelativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge, behavior: NSPopoverBehavior)
Run Code Online (Sandbox Code Playgroud)

NSViewController呈现另一视图控制器作为酥料饼.

当您这样做时,可通过presenting酒店提供呈现的视图控制器.因此,您只需要调用 presenting?.dismissViewController(self)即可关闭popover.

我希望这对寻求这个问题的现代解决方案的任何人都有帮助.


Ken*_*ses 8

你有没有看过NSPopover文件?它有一种-close方法,并且为了略有不同的目的,-performClose:方法.