本文主要是介绍在 往CCLayer里面加 Notification通知的时候,一定要加在OnEnter和OnExit里面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在 往CCLayer里面加 Notification通知的时候,一定要加在OnEnter和OnExit里面,不要加在init和 dealloc上面,因为
很多时候CCLayer是通过node方法产生的,在切换layer的时候,dealloc不一定会调用,这样会导致一个layer多次注册一个通知,这样导致,如果发送一个通知,会多次调用通知响应函数,导致问题,切记。
例子代码如下:
-(void) onEnter
{//注册新玩家读取通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReloadPlayerVec:) name:Str_NSNotification_FetchPlayerChairInfo object:nil];//注册新用户基本资料通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewUserBasicInfo:) name:Str_NSNotification_NewUserBasicInfo object:nil];//注册新用户其他资料通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewUserOtherInfo:) name:Str_NSNotification_NewUserOtherInfo object:nil];//注册玩家退出当前桌子的通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPlayerExitTable:) name:Str_NSNotification_PlayerExitTable object:nil];//注册 公聊 消息 通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPublicMsg:) name:Str_NSNotification_PublicMsg object:nil];[super onEnter];
}- (void) onExit
{[[NSNotificationCenter defaultCenter] removeObserver:self name:Str_NSNotification_FetchPlayerChairInfo object:nil];[[NSNotificationCenter defaultCenter] removeObserver:self name:Str_NSNotification_NewUserBasicInfo object:nil];[[NSNotificationCenter defaultCenter] removeObserver:self name:Str_NSNotification_NewUserOtherInfo object:nil];[[NSNotificationCenter defaultCenter] removeObserver:self name:Str_NSNotification_PlayerExitTable object:nil];[[NSNotificationCenter defaultCenter] removeObserver:self name:Str_NSNotification_PublicMsg object:nil];[self clearUp];[super onExit];
}
这篇关于在 往CCLayer里面加 Notification通知的时候,一定要加在OnEnter和OnExit里面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!