如何在UITabBarController中的两个UIViewController之间传递信息 [英] How to pass information between two UIViewControllers in a in a UITabBarController

查看:67
本文介绍了如何在UITabBarController中的两个UIViewController之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone App中,有两个UIViewControllers.两者都嵌入在UITabBarController中.但是,当点击TabBarController并切换VC时,屏幕上的VC会将数据上传到NSUserDefaults,然后切换到的VC从NSUserDefaults获取该数据.

In my iPhone App, there are two UIViewControllers; both are embedded in a UITabBarController. However, when the TabBarController is tapped, and the VC's switch, the VC on screen uploads data to NSUserDefaults and then the VC that get switched to, fetches that data from NSUserDefaults.

我目前要做的是在第一个VC的viewWillDisappear中上传数据,然后在第二个VC的viewWillAppear中获取数据. 问题在于即将在屏幕上显示的VC的viewWillAppear在旧" VC的viewWillDisappear之前被调用,因此它试图获取尚未上传的数据.

What I currently do is upload data in viewWillDisappear of the first VC and then fetch it in viewWillAppear of the second VC. the problem is that viewWillAppear of the VC that is about to get put on screen is called before viewWillDisappear of the "old" VC so it tries to get data that isn't uploaded yet.

我可以使用什么,因此旧VC"是第一个知道何时将要退出屏幕的人,以便它可以在新VC获取数据之前上传数据?

What can I use so the "old VC" is the first one to know when it is about to go offscreen so it can upload the data before the new one fetches that data?

*我还尝试设置UITabBArControllerDelegate,以便旧的VC可以接收到-tabBarController:didSelectViewController :,但这也为时已晚.

*I also tried setting up a UITabBArControllerDelegate so the old VC would receive - tabBarController:didSelectViewController:but that gets called too late as well.

推荐答案

如果您不打算传递大量信息,则可以将默认变量用作众多可用选项之一.可能不是最好的,但是会起作用.

If you're not going to be transmitting a lot of information, you can make use of default variables as one of the many options available. It might not be the best, but it'll work.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:Variable forKey:@"variable"];
    [defaults synchronize];

设置变量

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *fetchVariable = [defaults objectForKey:@"variable"];

以便在另一个VC中检索它.

to retrieve it in the other VC.

这篇关于如何在UITabBarController中的两个UIViewController之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆