【问题标题】:OS X: Show interface of application agent (UIElement)OS X:显示应用程序代理的界面(UIElement)
【发布时间】:2012-11-08 19:15:30
【问题描述】:

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

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

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

【问题讨论】:

    标签: macos uielement


    【解决方案1】:

    你可以这样做:

    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];
    }
    

    【讨论】:

    • 这对我不起作用。但是,我确实在 Stack Overflow 上找到了另一个解决方案。
    【解决方案2】:

    我在这里找到了答案:Closing Mac application (clicking red cross on top) and reopening by clicking dock icon

    - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
        hasVisibleWindows:(BOOL)flag
    {
        [self.window makeKeyAndOrderFront:self];
        return YES;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 2014-03-23
      • 2015-09-27
      • 2010-12-14
      • 1970-01-01
      • 2011-04-16
      • 2011-09-20
      • 2017-09-12
      • 2014-11-25
      相关资源
      最近更新 更多