1

私のアプリ (ゲーム) では、センター/ホーム ボタンまたはロック ボタンが押されたときに、NSNotificationCenter を使用してゲームを一時停止および再開しようとしています。これは私が使用しているコードです:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(pauseLayer:)
 name:UIApplicationWillResignActiveNotification
 object:self.view.layer.sublayers];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(pauseLayer:)
 name:UIApplicationDidEnterBackgroundNotification
 object:self.view.layer.sublayers];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(resumeLayer:)
 name:UIApplicationWillEnterForegroundNotification
 object:self.view.layer.sublayers];

viewDidLoad、viewDidAppear、initWithNibNameOrNil などのさまざまな場所に配置して実験しましたが、それらはすべて呼び出されますが、アプリのデリゲート メソッドが呼び出しても、pauseLayer メソッドと resumeLayer メソッドは呼び出されません。このコードが機能しないのはなぜですか?

4

1 に答える 1

4

呼び出しを変更し、パラメーターからaddObserver削除します。に変更します。self.view.layer.sublayersobjectnil

編集:詳細

もちろん。objectパラメータは、NSNotificationCenter観察したいオブジェクトの通知を示します。指定すると、配列によって送信された et al のみself.view.layer.sublayersが観察されます。もちろん、アレイはこの通知を送信しません。指定すると、任意のオブジェクトからの通知を監視します。この場合は適切です。オブジェクトを指定する場合は、 になります。UIApplicationWillEnterForegroundNotificationsublayerssublayersobject:nil[UIApplication sharedApplication]

于 2011-11-23T00:07:53.627 に答える