2
votes

I created a project using tabbar apllication and storyboard.

Now i have FirstViewController (which is selected by default) and SecondViewController.

How do i catch the moment when the selected view controller changes? (the user selects second view controller on tabbar).

The only way i thought of is: 1. Make a property for UITabBarController in appDelegate.

  1. Set it using self.tabBarController = (UITabBarController *)self.window.rootViewController

  2. Make the first view controller conform to UITabBarControllerDelegate.

  3. Set the tabbar's delegate to FirstViewController using [[UIApplication sharedApplication] delegate]

Is there a simpler and cleaner way to do this?

1

1 Answers

1
votes

I don't follow the third step, but here's what I think you want to do:

Subclass tab bar controller and implement the delegate protocol:

@interface RootTabBarController : UITabBarController <UITabBarControllerDelegate>

and set its delegate = self in its own viewDidLoad. Implement:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController