简体   繁体   English

如何在UITabBarController中的两个UIViewControllers之间传递信息

[英]How to pass information between two UIViewControllers in a in a UITabBarController

In my iPhone App, there are two UIViewControllers; 在我的iPhone App中,有两个UIViewControllers; both are embedded in a UITabBarController. 两者都嵌入在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. 但是,当点击TabBarController和VC的开关时,屏幕上的VC会将数据上传到NSUserDefaults,然后将切换到的VC上传,从NSUserDefaults获取该数据。

What I currently do is upload data in viewWillDisappear of the first VC and then fetch it in viewWillAppear of the second VC. 我目前所做的是在第一个VC的viewWillDisappear中上传数据,然后在第二个VC的viewWillAppear中获取它。 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的viewWillAppear会在“旧”VC的viewWillDisappear之前调用,因此它会尝试获取尚未上传的数据。

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? 我可以使用什么,所以“旧VC”是第一个知道什么时候要离屏的人,所以它可以在新数据取出数据之前上传数据?

*I also tried setting up a UITabBArControllerDelegate so the old VC would receive - tabBarController:didSelectViewController:but that gets called too late as well. *我也试过设置一个UITabBArControllerDelegate,这样老VC就会收到 - tabBarController:didSelectViewController:但是调用得太晚了。

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

To set the variables 设置变量

and

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

to retrieve it in the other 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? 我可以使用什么,所以“旧VC”是第一个知道什么时候要离屏的人,所以它可以在新数据取出数据之前上传数据?

You can try using the – tabBarController:didSelectViewController: UITabBarControllerDelegate method to save the data in NSUserDefaults ; 您可以尝试使用– tabBarController:didSelectViewController: UITabBarControllerDelegate方法将数据保存在NSUserDefaults ; this should be certainly called before viewWillAppear in any controller. 这应该在任何控制器中的viewWillAppear之前调用。

If you want to keep your current approach, try using viewDidAppear: instead of viewWillAppear: in the second view controller. 如果您想保留当前的方法,请尝试在第二个视图控制器中使用viewDidAppear:而不是viewWillAppear: This should work properly if you are not fetching a lot of data from NSUserDefaults (as it should be the case, I guess) and if your second view controller UI does no introduce a delay in showing the data. 如果你没有从NSUserDefaults获取大量数据(我认为应该是这种情况),并且你的第二个视图控制器UI没有引入显示数据的延迟,这应该可以正常工作。

Another approach you have is making the first controller update its data in NSUserDefaults each time it changes. 另一种方法是让第一个控制器每次更改时在NSUserDefaults更新其数据。

Writing and reading back sounds like an unnecessary overhead. 写作和回读听起来像是不必要的开销。 Unless you need to write it anyway, of course. 当然,除非你需要写它。

I'd make the previous UIViewController pass all data wrapped in an object to the UITabBarController , which would then pass it to the new UIViewController . 我让之前的UIViewController将包含在对象中的所有数据传递给UITabBarController ,然后UITabBarController将它传递给新的UIViewController If this is a pattern for all (or most of) your tabs, create a new protocol and let your UIViewController s implement that. 如果这是所有(或大部分)选项卡的模式,请创建一个新协议,让UIViewController实现它。 Your UITabBarController would then be able to figure out which controllers need it by simply checking if the controller conforms to your protocol. 然后,您的UITabBarController将能够通过检查控制器是否符合您的协议来确定哪些控制器需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM