0
votes

How can I call the same ViewController from two TabBar items?

I used:

MyViewController *vc1 = [[MyViewController alloc] initWithNib:@"MyViewController" bundle:nil]; MyViewController *vc2 = [[MyViewController alloc] initWithNib:@"MyViewController" bundle:nil];

UITabBarController *tabs = [[UITabBarController alloc] init]; [tabs setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];

But variables will be reset, I want to keep the variables.

My view controller contains TableView with listdata, I just want to show first three of them when I click one 2nd TabBar, but listdata variable is reset and become nil.

Any fix? Thanks

2
So you want to show the same thing in two tabs? Why have two separate tabs then? - SVD
Because one is for showing all data in tableView, and other one is for showing specific data in tableView (e.g. Favorite items) - Houranis

2 Answers

0
votes

I'm not sure why you'd do this, but this probably works.

MyViewController *vc1 = [[MyViewController alloc] initWithNib:@"MyViewController" bundle:nil]; 

UITabBarController *tabs = [[UITabBarController alloc] init];
[tabs setViewControllers:[NSArray arrayWithObjects:vc1, vc1, nil] animated:NO];
0
votes

You can have one of the view controllers be a subclass of another, and the subclass would have some data filtered out. UITabBarController has a delegate method that it calls when switching between tabs - you can use that to populate the lists as needed.