Can [self.window makeKeyAndVisible]; be called before setting rootviewcontroller

S.J picture S.J · Jul 18, 2013 · Viewed 8k times · Source

My requirement is that UITabBarController is the rootviewcontroller and on very first time of app launch I want to show login procedure which is inside UINavCon, and I am displaying it through presentViewController.

I dont want the UITabBarController visible for first time and dont want to how login UINavCon popping as modal.

I want to make user experience that if app starts for first time login UINavCon should be visible. So here is my code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

[self.window makeKeyAndVisible];//is it correct to call it here?

LoginVC *loginObj = [[LoginVC alloc]init];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:cellPhoneNumber];

self.tabBarController = [[UITabBarController alloc]init];

self.window.rootViewController = self.tabBarController;

[self.tabBarController presentViewController:self.navigationController animated:NO completion:^{}];

return YES;
}

I am calling [self.window makeKeyAndVisible]; on second line right after uiwindow alloc init. Is it correct do this or I can experience problems like viewcontroller not receiving events or orientations notifications?

Answer

HRM picture HRM · Jul 18, 2013

You haven't mentioned that whether you got the code working or not by using your implementation. Anyways I have done similar kind of implementation recently where we need to present login controller and then tabBarController after logging in, so just sharing my implementation.

  1. Create your login controller and present it in didFinishLaunching method.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    LoginController *loginCObj= [[[MainScreenController alloc]init]autorelease];
    UINavigationController *navigationControllerObj = [[[UINavigationController alloc]initWithRootViewController:loginObj]autorelease];
    self.window.rootViewController = navigationControllerObj;
    [self.window makeKeyAndVisible];
    
  2. After that on succesful login in your login view controller, call an appDelegate public method

    In login controller

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDel  applicationLoggedInSuccesfully];
    

    In your appDelegate file, add a method like this:

    -(void)applicationLoggedInSuccesfully{
        UINavigationController *nv1 = [[[UINavigationController alloc] initWithNibName:nil bundle:nil]autorelease];
        TabController1 *v1 = [[[TabController1 alloc] initWithNibName:nil bundle:nil]autorelease];
        [nv1 pushViewController:v1 animated:NO];
    
        UITabBarController *tabController = [[[UITabBarController alloc] init]autorelease];
        tabController.viewControllers = @[nv1];
        tabController.delegate = self;
        self.window.rootViewController = tabController;
        [self.window makeKeyAndVisible];
    }
    

Hope it will help you.