如何为“应用程序是代理(UIElement)”设置为“是”的应用程序重新显示?
界面在我第一次启动应用程序时显示,但是如果我关闭窗口,然后单击应用程序的图标,则没有任何反应。我想这是因为OS X正在尝试再次启动应用程序,并且有一些机制阻止了这一点。我想要的是这个:
答案 0 :(得分: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];
}
答案 1 :(得分: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;
}