I want to catch the event when someone switches between tabs. I have the following two function in my appdelegate file:
- (BOOL)application:(UIApplication *
You need your AppDelegate to conform to UITabBarControllerDelegate
protocol. See below..
@interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>
in my appdelegate.h file, I changed the line
@interface wscAppDelegate : UIResponder <UIApplicationDelegate>
to
@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
Then in my CustomTabBarController in the viewDidLoad function i added these lines:
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
Then in appdelegate.m file, I added this function
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"hooray this works");
}
I also struggled with this a lot but my UITabBarNavigationController is pretty far from AppDelegate to set it there, and the only way to actually make it work was to do it not in the viewDidLoad of the UITabBarController subclass itself, but in its:
-(void)viewDidLayoutSubviews {
[ super viewDidLayoutSubviews ];
self.delegate = self;
}
And that solved it. Alternatively you can do it in any of the tab bar controlled viewcontrollers, but that just feels wrong.