iOS NSNotification 通知傳值

2021-07-22 10:33:21 字數 1325 閱讀 1596

標籤: 介面

ios開發

iosnsnotification通知

2015-11-27 16:42

370人閱讀收藏 

舉報

思路:第三個介面的值傳給第乙個介面。

1. 在第乙個介面建立乙個通知中心, 通過通知中心,註冊乙個監聽事件

2. 在第乙個介面中,設定接收到通知的事件。

3. 在第乙個介面中的dealloc中, 將通知中心remove掉

4. 在第三個介面中, 建立乙個通知中心, 通過通知中心, 傳送通知(傳送通知的過程就是傳值的過程,將要傳輸的值作為object的值傳給第乙個介面 

**片段:

第一介面:

//通知中心是個單例

nsnotificationcenter

*noticenter = [

nsnotificationcenter

defaultcenter

];

// 註冊乙個監聽事件。第三個引數的事件名, 系統用這個引數來區別不同事件。

[noticenter 

addobserver

:self

selector

:@selector

(receivenotification:) 

name

:@"cesuo"

object

:nil

];

// @selector

(receivenotification:)方法, 即受到通知之後的事件

- (void

)receivenotification:(

nsnotification

*)noti

// 第一介面中dealloc中移除監聽的事件

- (void

)dealloc

第二介面:

// 建立乙個通知中心

nsnotificationcenter

*center = [

nsnotificationcenter

defaultcenter

];

// 傳送通知. 其中的name填寫第一介面的name, 系統知道是第一介面來相應通知, object就是要傳的值。 userinfo是乙個字典, 如果要用的話,提前定義乙個字典, 可以通過這個來實現多個引數的傳值使用。

[center 

postnotificationname

:@"cesuo"

object

:@"zhangheng"

userinfo

:dic];

NSNotification 通知傳值

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

iOS通知NSNotification傳值的使用

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

通知傳值的使用

輸入所要傳送的資訊 同時將label的值通過button方法呼叫傳遞,ibaction buttonclick id sender 在傳送通知後,在所要接收的控制器中註冊通知監聽者,將通知傳送的資訊接收 void viewdidload void tongzhi nsnotification tex...