首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotificationCenter addObserver没有响应

NSNotificationCenter addObserver没有响应
EN

Stack Overflow用户
提问于 2011-11-23 08:01:21
回答 1查看 5.1K关注 0票数 1

在我的应用程序(游戏)中,当中心/主页或锁定按钮被按下时,我尝试使用NSNotificationCenter暂停和恢复游戏。这是我使用的代码:

代码语言:javascript
复制
[[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方法从来没有被调用过,即使是应用委托方法也是如此。为什么这段代码不能工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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]

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8235630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档