iOS六种传值方式之通知传值

来源:互联网 发布:软件外包行业现状 编辑:程序博客网 时间:2024/05/23 19:17

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