Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa Popover NSStatusItem_Cocoa_Nsstatusitem_Nspopover - Fatal编程技术网

Cocoa Popover NSStatusItem

Cocoa Popover NSStatusItem,cocoa,nsstatusitem,nspopover,Cocoa,Nsstatusitem,Nspopover,我在玩弄一个想法,基本上我想要一个带有NSPopoverController的NSStatusItem。我读到了人们遇到的所有问题,但我只是想尝试一下。现在有没有干净的方法?我见过的所有版本都至少有一年的历史了,而且都是超级黑客 到目前为止,这是我的方法,但如果我在状态栏中单击我的应用程序,什么都不会发生 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { self.statusItem = [[

我在玩弄一个想法,基本上我想要一个带有NSPopoverController的NSStatusItem。我读到了人们遇到的所有问题,但我只是想尝试一下。现在有没有干净的方法?我见过的所有版本都至少有一年的历史了,而且都是超级黑客

到目前为止,这是我的方法,但如果我在状态栏中单击我的应用程序,什么都不会发生

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

    //[self.statusItem setView:view];
    [self.statusItem setTitle:@"Test"];
    [self.statusItem setHighlightMode:YES];
    [self.statusItem setAction:@selector(activatePopover:)];

}


-(IBAction)activatePopover:(id)sender
{
    BOOL isEnabled = NO;

    if (isEnabled) {
        [self.popover showRelativeToRect:NSMakeRect(0, 0, 50, 50) ofView:statusItem.view preferredEdge:NSMinYEdge];

    } else {
        [self.popover close];
    }
}
有什么办法让它运行吗


谢谢

如果不在状态项上使用自定义视图,这将无法工作。如果未设置自定义视图,则
视图
属性将为空(它仅返回自定义视图,而不是仅使用
setTitle
时内部使用的任何视图
NSStatusItem

不幸的是,根据苹果的文档,如果你想使用
nspover
,你需要提供自己的视图并自己处理点击

我还没有看到一个完整的示例包含正确的处理方法(状态项的默认实现需要手动完成很多工作),并且还修复了popover wonkynesses:

  • 默认情况下,
    nspover
    不会成为键窗口(某些控件不起作用),除非覆盖nspover窗口的
    可以成为键窗口
  • 正确关闭其他状态项的菜单(您可以使用空菜单调用
    popUpStatusItemMenu
    ,以正确聚焦状态项)
  • 使用
    drawStatusBarBackgroundInRect
  • 对鼠标左键和右键单击做出反应
  • 使用
    NSRunningApplication.currentApplication.activateWithOptions
    确保状态项的所有窗口都处于活动状态(否则您的popover将不稳定地无法接收键盘输入)
  • 使用
    NSEvent.addGlobalMonitorForEventsMatchingMask
    解除NSPover(附带的内置解除机制Popover不适用于状态项)
  • 使用
    NSStatusBar.systemStatusBar.removeStatusItem
我希望不久能有一篇关于这一点的博文发表(注意:我使用的是RubyMotion,而不是Objective-C),它解释了所有这些问题,并希望为创建菜单集提供一个更简单的基础。如果我写那篇文章,我会更新这条评论。

代码:

-(void)initializeStatusBarItem
{
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
    NSImage* image = [NSImage imageNamed:@"image"];
//    [image setTemplate:YES];
    self.statusItem.button.image = image;
    self.statusItem.highlightMode = NO;
    self.statusItem.button.action = @selector(statusBarItemDidClick:);
}

- (void)statusBarItemDidClick:(NSStatusBarButton *)sender{
    MainViewController *mainView = [[MainViewController alloc] init];
    self.popoverView = [[NSPopover alloc] init];
    [self.popoverView setContentViewController:mainView];
    self.popoverView.contentSize = CGSizeMake(300, 400);
    self.popoverView.behavior = NSPopoverBehaviorTransient;
    [self.popoverView showRelativeToRect:sender.bounds ofView:sender preferredEdge:NSMaxYEdge];
}

从不调用
showRelativeToRect:ofView:preferredEdge
,因为
isEnabled
设置为NO.
2014-04-29 17:14:08.377 Aves[610:303]-[nspover showRelativeToRect:ofView:preferredEdge:]:未提供视图。您必须提供一个视图。
我已将
popover
定义为
@property(strong)IBOutlet nspover*popover