在我的应用程序(游戏)中,当中心/主页或锁定按钮被按下时,我尝试使用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方法从来没有被调用过,即使是应用委托方法也是如此。为什么这段代码不能工作?
发布于 2011-11-23 08:07:54
更改addObserver调用并从object参数中删除self.view.layer.sublayers。将其更改为nil。
编辑:更多信息
好的。object参数告诉NSNotificationCenter您想要观察哪个对象的通知。当您指定self.view.layer.sublayers时,您将观察到UIApplicationWillEnterForegroundNotification等人仅由sublayers数组发送。当然,sublayers数组不会发送此通知。当您指定object:nil时,您正在观察来自任何对象的通知。这在这种情况下是合适的。如果你想指定一个对象,它应该是[UIApplication sharedApplication]。
https://stackoverflow.com/questions/8235630
复制相似问题