我想以编程方式触发Safari Extension toolbarItem上的"click"事件,以便在网页上发生某些事情后出现我的自定义弹出窗口.我正在使用新的Xcode扩展IDE,并使用界面构建器构建了我的popover.StackOverflow上的所有答案目前都处理Safari扩展构建器中内置的扩展,而不是Xcode接口中的扩展.例如,我尝试过注入Safari JS解决方案,例如:
safari.extension.toolbarItems[0].showPopover();
Run Code Online (Sandbox Code Playgroud)
但没有任何反应,我不认为它在Xcode中构建扩展时应该可行.我不关心弹出窗口是在注入的javascript中还是在我的SafariExtensionHandler.Swift文件中触发 - 我只需要一种方法来显示它而无需用户单击工具栏项.
我使用扩展名的info.plist将我的popover与我的工具栏项相关联,我已将其链接到下面:
https://i.stack.imgur.com/VxyLs.png
该扩展与本机CocoaOS应用程序捆绑在一起,并使用在WWDC '15(下面的链接)中引入的新Xcode范例构建.我可以使用以下方法访问SafariExtensionHandler.Swift中的Toolbaritem:
func getToolbarItem(completionHandler: @escaping (SFSafariToolbarItem?) -> Void) {
SFSafariApplication.getActiveWindow {$0?.getToolbarItem (completionHandler: completionHandler)}
}
Run Code Online (Sandbox Code Playgroud)
但toolbarItem对象似乎没有显示弹出窗口的方法.
我想知道如何以编程方式关闭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]使它从一个按钮或快捷方式关闭,任何帮助使它工作,我很感激...