iosuitabbarcontrollerdismissrootview

Changing root view controller in the background


I have the following view controllers stack.

First, my app will show an app tour page. (Say TourViewController - super class is UIViewController). Added this controller in AppDelegate as rootviewcontroller.

self.window.rootViewController = tourViewController;

Then from the tour page, if the user taps on "Signin" button, I'm presenting the second view controller (Say LoginViewController - super class is UIViewController).

UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:self.loginViewController];
[self presentViewController:loginNavigationController animated:YES completion:nil];

After a successful login, I need to resign the second view controller (LoginViewController) and want to show a tab bar based view for further needs.

I tried this code inside the login success method.

[self dismissViewControllerAnimated:YES completion:^{

        TabBarViewController *tabController = [[TabBarViewController alloc] init];

        [self presentViewController:tabController animated:NO completion:nil];

        AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
        applicationDelegate.window.rootViewController = tabController;

    }];

Problems:

What I need is, When I resign the LoginViewController, the background view should be tab bar controller instead of TourViewController.

Help needed!!


Solution

  • u can change the root view controller in AppDelegate not in the success method of the loginNavigationController better u can do this way

    in AppDelegate.h

     #import <UIKit/UIKit.h>
     #import "TabControllerViewController.h"
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    - (void)showTabController;  //add this method call from on success method of log in completion 
    @end
    

    in AppDelegate.m

    - (void)showTabController;
     {
       TabControllerViewController *tabController =    [[TabControllerViewController alloc]  initWithNibName:@"TabControllerViewController" bundle:nil];
      self.window.rootViewController = tabController;
     [self.window makeKeyAndVisible];
    }
    

    and in loginNavigationController.m

    [self dismissViewControllerAnimated:YES completion:^{
    
        //TabBarViewController *tabController = [[TabBarViewController alloc] init];
    
      //  [self presentViewController:tabController animated:NO completion:nil]; //no nee to present 
    
        AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
         [applicationDelegate showTabController]; //there is no need to create a tab bar in loginview controller, create it in root view controller 
        //applicationDelegate.window.rootViewController = tabController;
    
    }];
    

    NOTE: above is not tested just try it once

    Edit 1

    one way u can do it but with different animation, form this answer u can change to second window by doing some animation for example

    in in AppDelegate.h

    #import <UIKit/UIKit.h>
    #import "TabViewController.h"
    #import "LoginViewController.h"
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window; //holds initial window, holds tour and login controller
    @property (strong, nonatomic) UIWindow *tabWindow; //holds only tab controller 
    //..other code below is my test 
    @property (strong, nonatomic) TabViewController *tabViewController;
    @property (strong, nonatomic) LoginViewController *loginController;
    - (void)showTabController;
    @end
    

    in AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    {
       _tabWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
       _window    = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
       // Override point for customization after application launch.
       _loginController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
       _tabViewController = [[TabViewController alloc] initWithNibName:@"TabViewController" bundle:nil];
    
        self.window.rootViewController = _loginController; //for test for your case it contains tour view controller  
        [self.window makeKeyAndVisible];
        return YES;
     }
    
     - (void)showTabController;
     {
        [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        self.window.rootViewController = _tabViewController;
        } completion:^(BOOL finished) {
          // [_tabWindow makeKeyAndVisible];
        }];
     }