六種傳值方式之通知傳值

A、B兩個頁面,需要將B上的值獲取并傳到A頁面上顯示出來。

通知傳值

通知傳值,在B頁面發(fā)送一個通知出去,A頁面接收這個通知,然后修改相關(guān)屬性的值,并將其顯示出來。

實現(xiàn)思路:

1.B頁面定義并發(fā)送一個通知

//定義一個全局變量 ------> 通知類型
#define kTextFieldTextChangeNotification @"TextFieldTextChangeNotification"
//在button的點擊事件里面
//1) 取值
UITextField *textField = (UITextField *)[self.view viewWithTag:2000];
//2) ***發(fā)送通知***
NSDictionary *userInfo = @{@"text" : textField.text};    
[[NSNotificationCenter defaultCenter] postNotificationName:kTextFieldTextChangeNotification object:nil userInfo:userInfo];
//3) 關(guān)閉模態(tài)視圖
[self dismissViewControllerAnimated:YES completion:nil];

2.回到A頁面后在接受通知的方法里面進行值的修改

#pragma mark - 接受通知的方法
- (void) receiveNotification: (NSNotification *) notification { 
      //1) 通過tag獲取頁面的label 
      UILabel *label = (UILabel *) [self.view viewWithTag:1000]; 
      //2) 修改label上的值 
      label.text = notification.userInfo[@"text"];
  }

3.移除監(jiān)聽對象

  - (void) dealloc { 
      [[NSNotificationCenter defaultCenter] removeObserver:self];
  }

源代碼地址:
https://github.com/zoushuyue/NSNotificationByValue

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容