A疆瑰、B兩個(gè)頁面眉反,需要將B上的值獲取并傳到A頁面上顯示出來昙啄。
通知傳值
通知傳值,在B頁面發(fā)送一個(gè)通知出去寸五,A頁面接收這個(gè)通知梳凛,然后修改相關(guān)屬性的值,并將其顯示出來梳杏。
實(shí)現(xiàn)思路:
1.B頁面定義并發(fā)送一個(gè)通知
//定義一個(gè)全局變量 ------> 通知類型
#define kTextFieldTextChangeNotification @"TextFieldTextChangeNotification"
//在button的點(diǎn)擊事件里面
//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頁面后在接受通知的方法里面進(jìn)行值的修改
#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];
}