Re-assigning RootViewController after successful login

My iOS app opens with a login prompt. Once the user logs in, it switches to the main view.

In application:didFinishLaunchingWithOptions, I set the RootViewController to a LoginViewController. The LoginViewController has the AppDelegate as its delegate:

  • MobileVLCKit fails while compiling
  • Using extensions in separate .swift file
  • Adding a UIView's to the Subview which is already added to the MainView
  • Locking an object from being accessed by multiple threads - Objective-C
  • How to AirPrint multiple print formatters in one go?
  • iOS 8.3 - UITableView cell not aligned, indentation to the left
  • LoginViewController *login = [[LoginViewController alloc] init];
    [login setDelegate:self];
    [[self window] setRootViewController:login];

    If the login is successful, the LoginViewController calls the AppDelegate’s userDidLogin method:

    if([[self delegate] respondsToSelector:@selector(userDidLogin)]) {
        [[self delegate] userDidLogin];

    userDidLogin creates a new UINavigationController and assigns it as the RootViewController:

    - (void)userDidLogin
        MainRecordViewController *mainRecordViewController = [[MainRecordViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainRecordViewController];
        [[self window] setRootViewController:navController];

    By adding an NSLog to the LoginViewController’s dealloc method, it appears that the LoginViewController is destroyed at that stage and that execution continues as expected.

    I haven’t done anything to explicitly close the LoginViewController, just relied on the assumption that assigning a new RootViewController will mean that the old one disappears and is tidied away by ARC.

    Can I rely on that always being the case? Is this a sensible approach?

    Thanks in advance.


    Solutions Collect From Internet About “Re-assigning RootViewController after successful login”

    The documentation of the rootViewController property states:

    If the window has an existing view hierarchy, the old views are
    removed before the new ones are installed.

    So if you don’t keep your own reference to the LoginViewController, it will be destroyed.

    Perhaps this: RootViewController Switch Transition Animation is also interesting for you, as it describes how to switch the root view controller
    with an animation.