4

NSWorkspaceWillSleepNotification と NSWorkspaceDidWakeNotification の使用方法を学習するための簡単なアプリを作成しました。私の目標は、コンピューターがスリープおよびウェイクするときにメソッドを呼び出すことです。私が作成したアプリは、それに応じて各ラベルを変更します。アプリをビルドしたら、デスクトップから起動します。アプリケーションを起動したら、コンピューターをスリープ状態にします。コンピュータが起動しても、アプリケーションのラベルは変更されません。ウィンドウに IBAction ボタンを追加して、ラベルが変更されるようにしました。ボタンを押すと、ラベルが実際に変わります。しかし、スリープとウェイク時にこのようなことが自動的に行われるようにしたいと考えています。私は何を間違っていますか?

#import "Controller.h"

@implementation Controller

- (void)fileNotifications {

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
                                                           selector: @selector(makeSleep:) 
                                                               name: NSWorkspaceWillSleepNotification 
                                                             object: nil];

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
                                                           selector: @selector(makeWake:) 
                                                               name: NSWorkspaceDidWakeNotification 
                                                             object: nil];
}

- (IBAction)makeSleep:(id)sender {
    [sleepLabel setStringValue:@"Computer Slept!"];
}

- (IBAction)makeWake:(id)sender {
    [wakeLabel setStringValue:@"Computer Woke!"];
}


@end
4

1 に答える 1

2

[[NSWorkspace sharedWorkspace] notificationCenter]使用してみる代わりに[NSNotificationCenter defaultCenter]

このような:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeSleep:) NSWorkspaceWillSleepNotification object:nil ];

[[NSNotificationCenter defaultCenter] addObserver:self @selector(makeWake:) NSWorkspaceDidWakeNotification object:nil ];

上記は正しくありません。https://developer.apple.com/library/mac/qa/qa1340/_index.htmlを参照してください。

使用[[NSWorkspace sharedWorkspace] notificationCenter]が必要です。

メソッドにオブザーバーを追加する必要があります- (void)awakeFromNib

于 2012-03-02T15:22:05.063 に答える