1

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • 1
    what is the message error? (I have did this before, in my case I did a popToRootViewControllerAnimated in the current NavigationController) – Ulysses Feb 28 '16 at 14:56
  • If if i use `popToRooTViewController` then it take me to current root. my new root is not working – Rahul Feb 28 '16 at 14:57
  • 1
    use setViewControllers, it Replaces the view controllers currently managed by the navigation controller with the specified items. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/setViewControllers:animated: – Ulysses Feb 28 '16 at 14:59
  • so what you are asking is to set view controller for your navigation controller ? – Teja Nandamuri Feb 28 '16 at 15:02
  • @TejaNandamuri No...I want to take user back to first screen..and make that screen root view – Rahul Feb 28 '16 at 15:05
  • can u explain about your error after this code ? you can do it very easy ! please explain more – Mo Farhand Feb 28 '16 at 15:20

1 Answers1

2

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];
souvickcse
  • 7,742
  • 5
  • 37
  • 64