Question

I have 4 tab bar items (the buttons at the bottom of the screen) and I need the last one to do display a different view depending on certain properties.

If the user has a specific property, touching this tab bar item will move on to a UINavigationController. If the user does not have a specific property, touching this tab bar will move on to a UIViewController with a WebView.

I'm using Storyboard and targeting iOS6.

Is this possible?

Thanks.

Was it helpful?

Solution

I don't it's possible to have a tab bar item dynamically move to different UIViewControllers. What you could do, however, is have two separate view controllers that have identical-looking UITabBarItems, and add/remove them appropriately by modifying the viewControllers property of the UITabBarController. According to https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/viewControllers, no animation occurs when changing the viewControllers property, so to the user it should appear as though nothing happened when the viewControllers changed, but the UITabBarItem will now redirect to a different UIViewController.

OTHER TIPS

First, in your Storyboard, make a relationship between your UITabBarController and a UINavigationController (we will be using the same UINavigationController for the fourth bar item regardless if the user has our specialProperty). Make the rootViewController of the UINavigationController be the custom UIViewController that presets your UIWebView (you can hide the navigation bar in your storyboard if you choose).

Then, make a custom subclass of UITabBarController if you have not done so already and set your UITabBarController in your storyboard to use this class (in my example, we will call this class MyTabBarController). You will need to override prepareSegue:sender: in this class.

For example:

@interface MyTabBarController : UITabBarController
@end

@implementation MyTabBarController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // get destination UIViewController
    id controller = segue.destinationViewController;
    // check if the destination has a rootViewController property
    if ([controller respondsToSelector:@selector(setRootViewController:)]) {
        // check your special property
        if ([controller specialProperty]) {
            // init the root of your UINavigationController
            UIViewController *specialController = [[MySpecialViewController alloc] init];
            [controller setRootViewController:specialController];
            // if you are not using ARC, call [specialController release] here
            // if you hid the navigation bar for your default controller in storyboard, show it
            [controller setNavigationBarHidden:NO animated:NO];
        } else {
            // do some custom configuration of your controller without the special property
        }
    } else {
        NSLog(@"%@ does not have a root view controller to override.",controller);
    }
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top