I have an application which has 5 tabs on a TabBarController. For simplicity sake lets say they are Tab A, B, C, D, and E. Each tab takes the user to a TableViewController which is embedded in a Navigation controller. Each tab also has its own specific .h and .m files. The code for the most part is very similar between the 5 tabs. I want to do away with these 5 sets of class files and just use 1 set. This will make it much easier for me to make changes to the application (in 1 place instead of 5 places). How can I detect in the single implementation file which tab was selected? Once I know that I can put logic in place to render the tableview specifically for which tab was selected…
Another thing I should mention is that I need to detect the selected Tab in the TableViewController. The TabBarController is the point of entry for the application and I do not have a TabBarController subclass.
I tried this code in the TableViewController however it does not get accessed and/or used.
in .h file:
@interface MyController : UITableViewController <UITabBarDelegate>
in .m file:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//NSLog(@"selectedIndex: %d", self.tabBarController.selectedIndex);
NSLog(@"didSelectItem: %d", item.tag);
}
Easy, You already have the solution!
That means that any viewController you add to a tab bar controller has this property filled in by the system.
Then in the view controller you want for the tab you implement viewWillAppear
In light of the comments this property of tabBarController doesn’t seem to be reliable.
The problem you describe sounds like something that could solved by subclassing. Make a subclass of UIViewController for the code in common with each tab and then subclass your subclass for each tabs viewController to make modifications unique to the tab.
Alternatively you could load each tab with the same class but a different xib. You can set properties on your view controller in the “user defined runtime attributes” section in interface builder. Then in the viewWillAppear block just check the property set by the xib on that instance.