NSNotification通知的使用

2021-07-05 10:16:47 字數 1081 閱讀 3735

nsnotification通知傳值的使用

1、建立通知(建立在當前需要使用的頁面) [[

nsnotificationcenter

defaultcenter

] addobserver

:self

selector

:@selector

(notificationmethod:) 

name

:@"customname"

object

:nil

];2、傳送通知(傳送通知訊息,告訴當前頁面數值發生變化,改變當前頁面狀態)

//需要進行傳值操作

nsdictionary

*dict =[[

nsdictionary

alloc

] initwithobjectsandkeys:@(

count),

@"readcount"

, nil

];[[

nsnotificationcenter

defaultcenter

] postnotificationname

:@"customname"

object

:nil

userinfo

:dict];

//只通知發生狀態變化

[[nsnotificationcenter

defaultcenter

] postnotificationname

:@"customname"

object

:nil

];3、處理通知的事件,notificationmethod方法

4、在dealloc中銷毀通知物件

[[nsnotificationcenter

defaultcenter

] removeobserver

:self

name

:@"customname"

object

:nil

];小以文筆,多有不足,還望閱讀之人多多指教!一點點學習**,一點點的分享知識,每天進步一點點!**小子的滬上生活

NSNotification學習筆記

這是乙個觀察者模式。首先在你需要監聽的類中加入觀察者 void addobserver id observer selector sel aselector name nsstring aname object id anobject 這個觀察者在監聽到anobject傳送名字為aname的noti...

NSNotification 通知傳值

方法如下 在a頁面viewdidload中註冊乙個通知 objc view plain copy 註冊通知,進行傳值 name reloadvoewnotification 這個就是其唯一標示符.系統是用來做鑑別其惟一性,就是裝置標示符一樣 nsnotificationcenterdefaultce...

iOS通知NSNotification傳值的使用

通知 是在跳轉控制器之間常用的傳值 方式,除了 模式,通知更方便 便捷,乙個簡單的demo實現通知的跳轉傳值.輸入所要傳送的資訊 同時將label的值通過button方法呼叫傳遞,ibaction buttonclick id sender 在傳送通知後,在所要接收的控制器中註冊通知監聽者,將通知傳...