-1

In my AppDelegates 'didFinishLaunchingWithOptions' function, I have this code in there:

if(loggedIn != nil)
    {
        MainViewController *mvc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MainView"];
        [self.window setRootViewController:mvc];

    }

Second Attempt which didn't work:

if(loggedIn != nil)
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MainView"];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self.window setRootViewController:nav];

    }

The problem is that when the MainViewController loads, the NavigationHeader is missing. I've tried various methods online and instantiations that basically do the same thing to no avail. I have also tried created a whole new navigationController and adding my view to it, however, that fails as well.

Moe Bataineh
  • 1,032
  • 1
  • 12
  • 29

1 Answers1

2

Your setting MainViewController as your root, if this is not a navigation controller, there will be no header when it opens.

Instead create a UINavigationController, set MainViewController as its root and then set the navigation controller as the window root.

e.g.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"home"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];

or if you have the navigation controller inside the storyboard then instantiate that. Most likely the initial view controller.

e.g.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboardName" bundle:nil];
[self.window setRootViewController::[storyboard instantiateInitialViewController]];

not sure if it will be the initial viewController or not, that requires more info of your setup to know.

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • @MohamadBataineh which method did you use. Please edit your question and post your current code – Simon McLoughlin Dec 17 '14 at 11:09
  • I used your first example. – Moe Bataineh Dec 17 '14 at 11:11
  • @MohamadBataineh have you checked each variable to make sure its not nil? the storyboard, the viewController etc. – Simon McLoughlin Dec 17 '14 at 11:15
  • @MohamadBataineh Are you running other code somewhere? have you checked that the viewDidLoad, viewWillAppear etc is being called? The same code has been given before: http://stackoverflow.com/questions/10827010/selecting-alternative-first-view-controller-from-story-board-at-application-star and i'm using it myself and it works fine – Simon McLoughlin Dec 17 '14 at 11:25
  • It appears I made it mistake in my viewDidLoad. I needed to clear the navigation queue incase the user navigated from somewhere else in the website and forgot to remove the code. This is my bad. Sorry about that. – Moe Bataineh Dec 17 '14 at 11:28