六種傳值方式之通知傳值

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

通知傳值

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

實現思路:

1.B頁面定義并發送一個通知

//定義一個全局變量 ------> 通知類型
#define kTextFieldTextChangeNotification @"TextFieldTextChangeNotification"
//在button的點擊事件里面
//1) 取值
UITextField *textField = (UITextField *)[self.view viewWithTag:2000];
//2) ***發送通知***
NSDictionary *userInfo = @{@"text" : textField.text};    
[[NSNotificationCenter defaultCenter] postNotificationName:kTextFieldTextChangeNotification object:nil userInfo:userInfo];
//3) 關閉模態視圖
[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.移除監聽對象

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

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

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

推薦閱讀更多精彩內容