0

I need to set the view controller of the home screen as the the root of the navigation controller, no matter how the user reaches it (push/show or custom segue)

The following code put in the viewDidLoad of a view controller seems to have no effect with iOS 7:

[self.navigationController setViewControllers:@[self]];

The navigation stack does not change at all.

Have you ever experienced any similar issue?

Thanks, DAN

DAN
  • 919
  • 1
  • 6
  • 23

3 Answers3

0

Create global reference of navigation controller in App Delegate and then with the reference of appDelegate you can change you rootView controller anywhere just type below code :

[appDelegate.navigaitonController initWithRootViewController:viewController];

Note: Here viewController is new viewcontroller reference which you want to set as root of navigation controller .

Alok
  • 24,880
  • 6
  • 40
  • 67
  • UINavigationController has no setRootViewController method. Calling [appDelegate.navigationController setViewControllers:@[self]] in the viewDidLoad of the view controller has no effect as [self.navigationController setViewControllers:@[self]] – DAN Aug 19 '15 at 09:13
  • @DAN you can use initWithRootViewController at the place of setRootViewController . initWithRootViewController is instance method of navigation controller . – Alok Aug 19 '15 at 11:12
  • if still not working then let me know again with chunk of your code . I ,ll provide you solution for that . – Alok Aug 19 '15 at 11:14
  • Doing: appDelegate.navigationController = self.navigationController; [appDelegate.navigationController initWithRootViewController:self]; results in: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported – DAN Aug 19 '15 at 11:28
  • you can not set view controller as a root in same class . Let you have A view controller . You cant set A as a root view controller in viewDidLoad . It will give you exception – Alok Aug 19 '15 at 14:15
0

Call your UIViewController method from app delegate like this..try it out.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController* pp = [[ViewController alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:pp];
self.window.rootViewController = nav; or [self.window setrootviewcontroller=nav];
[self.window addSubview:[nav view]];
[self.window makeKeyAndVisible];
Ganesh Kumar
  • 708
  • 6
  • 25
  • I cannot open my view controller like that as it can be reached from different flows of more than one storyboard – DAN Aug 19 '15 at 09:14
  • @DAN check this link it may help you http://stackoverflow.com/questions/8297701/using-multiple-storyboards-in-ios – Ganesh Kumar Aug 19 '15 at 09:34
  • Thanks for your hint but my problem is not how to link storyboards, I'm trying to understand why the method setViewControllers does not work – DAN Aug 19 '15 at 09:47
  • @DAN may be your NavigationController has not been initialized and loaded properly .Check it out. – Ganesh Kumar Aug 19 '15 at 09:58
0

Only one line to make your current viewController as rootviewcontroller

[[[UIApplication sharedApplication] delegate] window].rootViewController = self;
Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
  • This changes the root view controller of the entire app but doesn't affect the navigation controller stack – DAN Aug 19 '15 at 09:15