Passing data between Tabs using delegate not working

被刻印的时光 ゝ 提交于 2019-12-06 14:57:17

Simply enough, as you already have the delegation code, only change the line where you create a SecondViewController object. Rather than creating a new one, you should have a reference to the one that the tab bar will show.

In the viewDidLoad of FirstViewController, Change

SecondViewController *svc = [[SecondViewController alloc]init];

to

//assuming you have 2 view controllers in the tab bar controller, first being FirstViewController
SecondViewController *svc = [self.tabBarController.viewControllers objectAtIndex:1]; 

and that should do it

How do you actually get to the second tab? You're creating an instance of it in viewDidLoad, but if you switch tabs, that instance won't be used to show on screen. The system will create another instance which it'll use. You can access this by accessing the tab bar controller's viewcontrollers property and checking for SecondViewController.

EDIT: You could also question your design. I don't know much about your app, but chances are your first tab shouldn't really be the delegate for your second tab. If you want to pass along data, consider using NSNotificationCenter or persistent storage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!