Suchi
Suchi

Reputation: 10039

iOS - is it possible to select view for a uitabbar at runtime?

I am assigning my view controller to my tab right after I create it. Is it possible to select the view that will show after the tab is clicked?

For eg //user clicks tab 1 if(hasMessages) //show view A else //show view B

Upvotes: 1

Views: 138

Answers (1)

Jere Käpyaho
Jere Käpyaho

Reputation: 1315

Yes, it is possible. You need to set a delegate for your tab controller:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;  // or whatever suitable class you have

This delegate needs to conform to the UITabBarControllerDelegate protocol.

In your delegate, implement tabBarController:didSelectViewController: and inside it, find out which view you want to present. Assuming your tab's root view controller is a navigation controller, then the delegate method implementation would be something like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    /* logic goes here */

    [viewController pushViewController:someNewVC animated:YES];
}

Upvotes: 1

Related Questions