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];
}