简体   繁体   中英

iOS: Call method in tabs viewController

I'm trying to come up with something simple, but i can't figure it out.

I have a UITabBarController and at one point I have to display another tab's UIViewController, and call a method in that new UIViewController, using data from the original UIViewController.

So basically I want to pass data to another UIViewController (that may not be initialized yet), and show the right tab.

If I use NSNotificationCenter , I'm not sure if the tab's UIViewController is initialized yet, and also it's a bit ugly to use delegation here.

What is a clean way to send and show data in the new tab?

更好的想法是在选项卡视图控制器中实现协议,视图控制器可以调用该协议来发送数据并设置任何其他视图控制器

int x = 1; //this is the view controller you want to go to.
MySecondViewController *secondViewController = (MySecondViewController *)self.tabBarController.viewControllers[x];
[secondViewController setDataObject:dataObject];
[self.tabBarController setSelectedIndex:x];

So what's happening is you're calling the controller from the tabBarController, it will initiate there if its not. Then you can set whatever you want on the controller, then you just switch to the selected tab.

You should have a data model of some sort (singleton?) that is accessible by both view controllers. When the second view controller is about to display its view, it should reference the data model to determine the data it should display.

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