iosobjective-cuinavigationcontrollerrootview

How to Change Root View from anywhere in appliction in iOS


I have developed an app without storyboard.

I have a functionality of Logout. When user click on logout then I have to redirect him to the sign in page and clear all navigation stack and make sign in page root view.

I am trying with this code but using this my app crashes:

 SignUpVC *main = [[SignUpVC alloc]initWithNibName:@"SignUpVC" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:main];

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

Solution

  • To change the root view of the application you can do it in this way

    SignUpVC *main = [[SignUpVC alloc]initWithNibName:@"SignUpVC" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:main];
    [[UIApplication sharedApplication].keyWindow setRootViewController:navController];