2012-02-12 63 views

回答

7

只是覺得:

- (void) receiveWakeNote: (NSNotification*) note 
{ 
    NSLog(@"receiveSleepNote: %@", [note name]); 
} 

- (void) fileNotifications 
{ 
    //These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    //with the default notification center. 
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
                  selector: @selector(receiveWakeNote:) 
                   name: NSWorkspaceDidWakeNotification object: NULL]; 
} 
+0

兩件事情:你想要的 「無」 不是 「NULL」 爲你的對象,你應該格式化你的答案是將你的代碼顯示爲格式化的代碼 - 現在它很難讀。但很好的回答你自己的問題! – 2012-02-12 09:39:50

+0

如何正確格式化代碼?我不知道所需的標籤...謝謝! – 2012-02-12 13:08:06

+0

看起來像帕拉格擊敗你。但是在將來,請查看編輯器中的按鈕。其中之一是一對大括號(「{}」)。使用它將選定的文本塊格式化爲代碼。 – 2012-02-12 18:18:18

2

爲SWIFT 3:

func onWakeNote(note: NSNotification) { 
    print("Received wake note: \(note.name)") 
} 

func onSleepNote(note: NSNotification) { 
    print("Received sleep note: \(note.name)") 
} 

func fileNotifications() { 
    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onWakeNote(note:)), 
     name: Notification.Name.NSWorkspaceDidWake, object: nil) 

    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onSleepNote(note:)), 
     name: Notification.Name.NSWorkspaceWillSleep, object: nil) 
} 
相關問題