在IOS开发过程中,我们经常会遇到页面间相互传值。push的页面传值很简单,相信大家都会,那么pop回来的页面如何传值呢?
我总结了3个方法,希望对大家有所帮助,写的如有不对,希望大家指正。
1.通知传值
在页面的pop代码前,发送一个通知,然后在返回的页面的中监视这个通知,收到后处理值。具体代码如下
pop返回页面
[[NSNotificationCenter defaultCenter] postNotificationName:@"userHeight" object:nil userInfo:@{@"minimum_height":heightFirsTextField.text,@"maximum_height":heightSecTextField.text,@"position":typeChooseArray,@"adress":adressChooseArray,@"time":timeChooseArray}];
[self.navigationController popViewControllerAnimated:YES];
其中userInfo是一个字典,需要传递的数据都可以往里扔,然后在接受数据的页面添加
//通知传值,用于接收筛选界面传过来的值
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserHeight:) name:@"PracticeScreenData" object:nil];
在getUserHeight函数的处理方法里,我们就可以获得上个页面传过来的值了。
通知传值有一个缺点,就是通知是在IOS消息队列中,响应需要时间,不能返回立即响应。下面的两种方法是可以立即响应的。
2.导航数组传值
在我们的导航数组中,找到上一个页面的指针,然后直接操作上一个页面的对象,改变他的值。
OrderDetailsGroupViewController *orderVC = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
orderVC.receiverLabel.text = [NSString stringWithFormat:@"%@",dictionary[@"name"]];
orderVC.phoneLabel.text = [NSString stringWithFormat:@"%@",dictionary[@"phone"]];
orderVC.addressLabel.text = [NSString stringWithFormat:@"%@%@",dictionary[@"address"],dictionary[@"full_address"]];
orderVC.adressIDStr = [NSString stringWithFormat:@"%@",dictionary[@"address_id"]];
[self.navigationController popViewControllerAnimated:YES];
其中OrderDetailsGroupViewController是上一个页面的类。我们在这里直接给它赋值。
3.AppDelegate传值
众所周知,AppDelegate是一个全局单例,我们可以在AppDelegate里创建变量,然后用
AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
获取到这个单例,然后在这里改变变量的值,再去别的页面取。
那么到这里,三种方法我就介绍完了,希望大家根据自己的需要选择。
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/21256.html
微信打赏
支付宝打赏
感谢您对作者Ace的打赏,我们会更加努力! 如果您想成为作者,请点我