加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NotificationCenter的基本使用

发布时间:2020-12-14 19:13:54 所属栏目:百科 来源:网络整理
导读:NotificationCenter的基本使用:/span init方法中:/span NotificationCenter::getInstance()-addObserver(this,callfuncO_selector(HelloWorld::call2),"abc",nullptr); //添加观察对象 auto b1 = MenuItemImage::create("CloseNormal.png","CloseNormal.pn
NotificationCenter的基本使用:</span>
init方法中:</span>
NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(HelloWorld::call2),"abc",nullptr); //添加观察对象  
	
	auto b1 = MenuItemImage::create("CloseNormal.png","CloseNormal.png",CC_CALLBACK_0(HelloWorld::call,this)); //按钮   
	auto menu = Menu::create(b1,nullptr);
	this->addChild(menu);

 
对应的方法:</span>
void HelloWorld::call()
{
log("call()");
NotificationCenter::getInstance()->postNotification("abc",this);
}


void HelloWorld::call2(Ref* obj)
{
log("call2()");
auto s = Sprite::create("CloseNormal.png");
s->setPosition(Vec2(100,100));
this->addChild(s);
}
</pre><pre name="code" class="cpp">最后:
HelloWorld::~HelloWorld()
{
<span style="white-space:pre">	</span>NotificationCenter::getInstance()->removeObserver(this,"abc");  //取消监听   
<span style="white-space:pre">	</span>//NotificationCenter::getInstance()->removeAllObservers(this);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读