Cocos2dx 学习笔记26 CCNotificationCenter的使用

在ios开发中,经常会使用到通知这种模式,在coscos2dx中也移植了这种模式,CCNotificationCenter

CCNotificationCenter被设计成了一个单例模式。其使用方法和OC中差不多,在ios开发中经常是再一个界面出现或初始化的时候,注册消息通知,在界面消失时取消通知。CCNotificationCenter的用法如下:

先来看下cocos2dx中注册通知的函数:

/** @brief Adds an observer for the specified target.
     *  @param target The target which wants to observe notification events.
     *  @param selector The callback function which will be invoked when the specified notification event was posted.
     *  @param name The name of this notification.
     *  @param obj The extra parameter which will be passed to the callback function.
     */
    void addObserver(CCObject *target, 
                     SEL_CallFuncO selector,
                     const char *name,
                     CCObject *obj);


由此可见我们该这样来添加一个观察者: 

CCNotificationCenter::sharedNotifCenter()->addObserver(this,callfuncO_selector(HelloWorld::menuCloseCallback), TONGZHI, NULL);  //注册消息


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    //移除监听事件
    CCLog("收到监听 并且移除!");
    CCNotificationCenter::sharedNotifCenter()->removeObserver(this, TONGZHI);
}




写好以后 只要在需要的地方抛出通知 即可


 
 CCNotificationCenter::sharedNotifCenter()->postNotification(TONGZHI,NULL);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值