【问题标题】:How to Close NSPopover programmatically如何以编程方式关闭 NSPopover
【发布时间】:2014-08-05 23:23:29
【问题描述】:

我想知道如何以编程方式关闭 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

我的 NSPopover 的代码在 NSViewController (CambiarTipoCuentaVC) 内部,但在里面我既没有 [self close] 也没有 [self performClose] 从按钮或快捷方式关闭它,任何帮助使它工作我会很感激...

【问题讨论】:

    标签: macos xcode5.1 nspopover


    【解决方案1】:

    我看到这篇文章并想分享我的解决方案。

    >macOS 10.10、Swift 4

    macOS 10.10 起可以调用

    presentViewController(_ viewController: NSViewController, asPopoverRelativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge, behavior: NSPopoverBehavior)
    

    NSViewController 上显示另一个视图控制器作为弹出框。

    当你这样做时,呈现的视图控制器可以通过presenting 属性获得。因此,您只需调用presenting?.dismissViewController(self) 即可关闭弹出框。

    我希望这对寻求现代解决方案的人有所帮助。

    【讨论】:

    • 在 Swift 4.2 中,它正在展示 ViewController?.dismiss(self) 但除此之外,谢谢你 - 我发现它比我的解决方案更流畅,只是快一点。
    【解决方案2】:

    您是否看过NSPopover 文档?它有一个-close 方法,以及用于稍微不同的目的的-performClose: 方法。

    【讨论】:

    • 问题是......我的所有编程都在 NSViewController 中,并且 [self close] 和 [self performClose] 都不会上传我的代码
    • 消息不必发送到self。鉴于您有对弹出框的引用,您可以使用该引用向它发送消息。
    【解决方案3】:

    你要关闭的是你的popover的窗口,所以你可以简单地添加

    @IBAction func closePopover(_ sender: Any) {
        self.view.window?.performClose(sender)
    }
    

    到你的弹出框的视图控制器,不需要子类化。

    (Xcode 10、macOS 10.13、Swift 4.1)

    编辑添加: 我现在使用presentingViewController?.dismiss(self) 尝试了 JanApotheker 的解决方案,它比我的更快更流畅,所以我不再推荐它。

    【讨论】:

    • 这很难找到,但我非常感激。
    【解决方案4】:

    我找到了方法,我将补充 ken Thomases 的答案

    我创建了一个名为 MyNSPopover 的 NSPopover 子类

    然后我添加了下一个代码:

    #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
    

    然后像这样将这个类添加到我的 NSPopover 中

    完成就可以了

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 2015-11-26
      • 2020-07-08
      • 2013-07-04
      • 2010-11-17
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多