4

如何使“应用程序是代理(UIElement)”设置为“是”的应用程序的界面重新出现?

该界面在我第一次启动应用程序时显示,但如果我关闭窗口,然后单击应用程序的图标,则什么也不会发生。我猜这是因为 OS X 试图再次启动应用程序,并且有一些机制阻止了它。我想要的是这样的:

  • 第一次单击应用程序的图标应启动应用程序并显示界面。
  • 如果界面已关闭(但应用程序仍在后台运行),则随后单击图标应该只会显示界面。
  • 如果界面已经显示,点击图标应该只是将窗口移到前台。
4

2 回答 2

2

这是您可以做到的一种方法:

1)将+ initialize方法添加到您的应用程序委托

+ (void)initialize
{
    // check if there is a running instance of your app
    NSArray * apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
    if ([apps count] > 1)
    {
        //post notification to it to update inteface
        [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"updateInterface" object:nil];
        //quit current instance of the app, coz you don't need two apps running continiously
        exit(0);
    }
}

2)注册您的应用程序以获取通知

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInterface:) name:@"updateInterface" object:nil];
}

3) 添加updateInterface方法

- (void)updateInterface:(NSNotification *)aNotification
{
    // handle your interface here
    // ....

    // move your app forward
    [NSApp activateIgnoringOtherApps:YES];
}
于 2012-11-12T12:04:04.510 回答
2

我在这里找到了答案:关闭 Mac 应用程序(单击顶部的红十字)并通过单击停靠图标重新打开

- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
    hasVisibleWindows:(BOOL)flag
{
    [self.window makeKeyAndOrderFront:self];
    return YES;
}
于 2012-11-12T22:13:04.100 回答