Cocos2d-x-------------------------------消息機制NotificationCenter

NotificationCenter 常用函數

(1)addObserver函數 (訂閱消息)

void addObserver(Ref * target,SEL_CallFuncO selector,const std::string & name,Ref * sender)

根據指定的target,添加觀察者.

Ref * target :要訂閱消息的主體

SEL_CallFuncO selector :消息回調函數

std::string & name:消息名稱

Ref * sender :要傳遞的數據

(2)removeObserver 函數 (取消訂閱)

void removeObserver(Ref * target,const std::string & name)

根據指定的target 和 name 移除觀察者.

Ref * target:取消訂閱消息的主體

const std::string & name:消息名稱

(3)postNotification函數(發佈消息)

void postNotification(const std::string & name)  根據某個名字發送一個通知事件.

const std::string & name: 消息名稱

void postNotification(const std::string & name,Ref * sender) 根據某個名字發送一個通知事件.

const std::string & name: 消息名稱

Ref * sender: 要傳輸的數據

如果addObserver和postNotification函數都傳遞了數據,並且不是同一個數據,那將無法發佈消息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章