简体   繁体   中英

How to pass information between two UIViewControllers in a in a UITabBarController

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.

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.

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?

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

To set the variables

and

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

to retrieve it in the other 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?

You can try using the – tabBarController:didSelectViewController: UITabBarControllerDelegate method to save the data in NSUserDefaults ; this should be certainly called before viewWillAppear in any controller.

If you want to keep your current approach, try using viewDidAppear: instead of viewWillAppear: in the second view controller. 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.

Another approach you have is making the first controller update its data in NSUserDefaults each time it changes.

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 . If this is a pattern for all (or most of) your tabs, create a new protocol and let your UIViewController s implement that. Your UITabBarController would then be able to figure out which controllers need it by simply checking if the controller conforms to your protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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