Objective c 应用程序启动时显示NSPopover

Objective c 应用程序启动时显示NSPopover,objective-c,pyobjc,nsstatusitem,nspopover,Objective C,Pyobjc,Nsstatusitem,Nspopover,我正在使用Pyobjc创建NSStatusItem。单击它时,我将显示一个nspover。这很好用。但是,我的要求是在应用程序启动时立即显示popover,而不需要用户执行任何操作。在finishLaunching中直接调用回调不起作用。有没有办法做到这一点?即使只需模拟单击NSStatusView,也已经足够了 class TestApp(NSApplication): def finishLaunching(self): # Make statusbar item

我正在使用Pyobjc创建NSStatusItem。单击它时,我将显示一个nspover。这很好用。但是,我的要求是在应用程序启动时立即显示popover,而不需要用户执行任何操作。在finishLaunching中直接调用回调不起作用。有没有办法做到这一点?即使只需模拟单击NSStatusView,也已经足够了

class TestApp(NSApplication):

    def finishLaunching(self):
        # Make statusbar item
        statusbar = NSStatusBar.systemStatusBar()
        self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
        self.statusitem.setTarget_(self)
        self.statusitem.setAction_('statusItemClicked:')
        self.icon = NSImage.alloc().initByReferencingFile_('app-icon.png')
        self.icon.setScalesWhenResized_(True)
        self.icon.setSize_((20, 20))
        self.statusitem.setImage_(self.icon)
        self.statusitem.setHighlightMode_(1)

        # self.statusItemClicked_(None)

    def statusItemClicked_(self, notification):
        self.viewController = SimpleXibDemoController.alloc().initWithWindowNibName_("Login")

        # Show the window
        self.viewController.showWindow_(self.viewController)
        rect = self.statusitem.valueForKey_('button').frame()
        self.viewController.popover.showRelativeToRect_ofView_preferredEdge_(rect, self.statusitem.valueForKey_('button'), NSMaxYEdge)

您必须等待视图显示
nspover
。在
NSViewController
子类中重写并在那里显示它。(或者,如果您的最小部署目标小于OS X Yosemite,则覆盖
loadView

我最终得到了一个略显粗略的解决方案。我有一种方法可以像这样定位popover:

- (IBAction)showPopover:(id)sender {
    [popover showRelativeToRect:self.statusItemView.bounds ofView:self.statusItemView preferredEdge:NSMinYEdge];
}
在ApplicationIDFinishLaunching或finishLaunching中,我没有直接调用该方法,而是使用PerformSelect调用它:

[self performSelector:@selector(showPopover:) withObject:self afterDelay:0];
即使将延迟设置为0也可以工作,我不知道为什么,现在它可以正确定位popover

编辑:这似乎只在特定情况下起作用。即使创建一个视图控制器并从ViewDidDisplay调用它,popover也只有一半的时间定位


编辑2:我在状态项的窗口中添加了一个窗口控制器,并覆盖了windowDidLoad。然而,事实证明,在我设置控制器的窗口之前,窗口已经加载,因此不会调用windowDidLoad。

我也有同样的问题。从finishLaunching定位popover似乎太早了,因为状态项框架尚未在菜单栏上定位,因此popover始终显示在屏幕的左下角。这种方法在下一次运行循环中安排演示。但是,视图不能保证在当时可见,并且在某些情况下可能会崩溃。我尝试将视图控制器添加到我的状态项视图中,但即使将状态项的视图设置为视图控制器的视图,也无法使视图显示在菜单栏中。你能举个例子说明如何做到这一点吗?