2017-10-06 73 views
1

我正在研究旨在跟蹤用戶使用的最前面窗口應用程序的Mac OS應用程序。該代碼是這樣的:當應用程序通過單擊紅色叉號並重新打開時,NSWorkspaceDidActivateApplicationNotification失敗

@interface MDAppController : NSObject <NSApplicationDelegate> { 
NSApplication *currentApp; 
} 
@property (retain) NSApplication *currentApp; 
@end 

@implementation MDAppController 
@synthesize currentApp; 

- (id)init { 
    if ((self = [super init])) { 
    [[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self 
     selector:@selector(activeAppDidChange:) 
     name:NSWorkspaceDidActivateApplicationNotification object:nil]; 
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self 
     selector:@selector(activeAppDidTerminate:) 
     name:NSWorkspaceDidTerminateApplicationNotification object:nil]; 
} 
return self; 
} 

NSWorkspaceDidActivateApplicationNotification失敗時的應用程序可通過單擊十字按鈕在左上角,然後重新打開關閉來通知。 如果我從碼頭退出應用程序並重新打開它,則NSWorkspaceDidActivateApplicationNotification正常工作。 任何建議爲什麼它不在以前的情況下工作?提前致謝。

回答

0

我沒有開發MacOs應用程序的任何經驗,但是點擊左上角的十字架時,應用程序不會退出。只有那個窗口關閉。您可能需要使用其他方法。

+0

是的,我明白了。當應用程序通過十字按鈕關閉時,它只會停用,但不會退出。在這種情況下,'NSWorkspaceDidDeactivateApplicationNotification'工作。但是,當我關閉它後重新打開應用程序時,NSWorkspaceDidActivateApplicationNotification無法通知。有什麼解決方法嗎? – Priyanka

+0

對不起,我對MacOs開發一無所知。這只是我注意到使用MacOs。 – pramjeet

相關問題