Macos 如何以编程方式关闭nspover
我想知道如何以编程方式而不是通过触摸外部来关闭NSPopover,因为我想将其分配给一个操作,如按下Enter键或其他快捷键 因为我用快捷方式打开nspover,所以按另一个命令关闭会更符合逻辑 将要共享我的代码: DedictionDecunteaswc.h NSWindowController,我从那里调用我的NSpoverMacos 如何以编程方式关闭nspover,macos,xcode5.1,nspopover,Macos,Xcode5.1,Nspopover,我想知道如何以编程方式而不是通过触摸外部来关闭NSPopover,因为我想将其分配给一个操作,如按下Enter键或其他快捷键 因为我用快捷方式打开nspover,所以按另一个命令关闭会更符合逻辑 将要共享我的代码: DedictionDecunteaswc.h NSWindowController,我从那里调用我的NSpover #import "EdicionDeCuentasWC.h" #import "CambiarTipoCuentaVC.h" @interface EdicionDeC
#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 CambiartiPocountAVC中,但在里面我没有通过按钮或快捷方式关闭它的[self-close]或[self-performClose],如果有任何帮助,我将不胜感激……您看过NSPopover文档了吗?它有一个-close方法,出于一个稍微不同的目的,-performClose:method。您是否已经查看了NSpover文档?它有一个非常接近的方法,出于一个稍微不同的目的,-performClose:method。我找到了方法,我将补充ken Thomases的答案 我创建了一个名为mynspover的nspover子类
#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
然后我添加了下一个代码:
#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
然后像这样把这个类添加到我的nspover中
我找到了方法,我要补充肯·托马斯的答案 我创建了一个名为mynspover的nspover子类
#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
然后我添加了下一个代码:
#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
然后像这样把这个类添加到我的nspover中
done just works我看到这篇文章,想与大家分享我的解决方案 >macOS 10.10,Swift 4 由于macOS 10.10,您可以拨打
presentViewController(_ viewController: NSViewController, asPopoverRelativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge, behavior: NSPopoverBehavior)
在NSViewController上,将另一个视图控制器显示为popover
执行此操作时,可以通过presenting属性使用presenting视图控制器。因此,您只需调用presenting?.dismissViewControllerself即可解除popover
我希望这对任何寻求现代解决方案的人都有帮助。我看到这篇文章,想与大家分享我的解决方案 >macOS 10.10,Swift 4 由于macOS 10.10,您可以拨打
presentViewController(_ viewController: NSViewController, asPopoverRelativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge, behavior: NSPopoverBehavior)
在NSViewController上,将另一个视图控制器显示为popover
执行此操作时,可以通过presenting属性使用presenting视图控制器。因此,您只需调用presenting?.dismissViewControllerself即可解除popover
我希望这对任何寻求现代解决方案的人都有帮助。您要关闭的是popover的窗口,因此您只需添加
@IBAction func closePopover(_ sender: Any) {
self.view.window?.performClose(sender)
}
对于popover的视图控制器,不需要子类化
Xcode 10、macOS 10.13、Swift 4.1
编辑以添加:
我现在用presentingViewController?尝试了JanApotheker的解决方案。它比我的更快、更流畅,所以我不再推荐它。您要关闭的是popover的窗口,因此您可以简单地添加
@IBAction func closePopover(_ sender: Any) {
self.view.window?.performClose(sender)
}
对于popover的视图控制器,不需要子类化
Xcode 10、macOS 10.13、Swift 4.1
编辑以添加:
我现在已经用presentingViewController?尝试了JanApotheker的解决方案。它比我的更快、更流畅,所以我不再推荐它。问题是。。。我所有的编程都在一个NSViewController中,没有[self close]或[self performClose]上传我的代码信息,也不必上传到self。假设你有一个对popover的引用,你可以使用该引用向它发送一条消息。。。我所有的编程都在一个NSViewController中,没有[self close]或[self performClose]上传我的代码信息,也不必上传到self。如果您有一个对popover的引用,您可以使用该引用向其发送一条消息。这很难找到,但我非常感谢。这很难找到,但我非常感谢。在Swift 4.2中,它是presentingViewController?。Dismisself,但除此之外,谢谢你-我发现它比我的解决方案更平滑,只比我的解决方案快一点。在Swift 4.2中,它是presentingViewController?的。请自行解散,但另外,谢谢你-我发现它比我的解决方案更平滑,只比我的解决方案快一点。