Shafqat
Shafqat

Reputation: 21

How to call tabbarviewcontroller from UIViewController?

I have a tabbar-based application with three tabs. Now when the device is rotated from from portrait to landscape it should load a different UIViewController and when application mode changes form landscape back to portrait mode then tabbar controller should be shown again. How can this be done?

Upvotes: 2

Views: 219

Answers (1)

Besi
Besi

Reputation: 22939

I guess you could approach the issue as follows with the following method. As you have two different ViewControllers which are on the same hierarchical level it makes sense to have one ViewController, which manages those two ViewControllers and shows the respective ViewController, depending on the orientation.

Portrait

  • window
    • MyRootViewController (UIViewController subclass)
      • MyTabBarViewController (UITabBarViewController subclass)

Landscape

  • window
    • MyRootViewController (UIViewController subclass)
      • MyPortraitViewController (UIViewController subclass)

Now your MyRootViewController class detects any rotation (see the UIViewController docs) and changes it's view to either of your two ViewControllers:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    // Set the view to the corresponding ViewController (Assuming they were already initialized)
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        self.view = self.myTabBarViewController.view;
    } else {
        self.view = self.myPortraitViewController.view;
    }
}

You should also make sure, that the correct ViewController is displayed on startup / re-activation of your App. For this you can use the following method in your MyRootViewController class:

- (void)viewWillAppear:(BOOL)animated;

I hope this helps

Upvotes: 1

Related Questions