bdc
bdc

Reputation: 188

App opens to specific ViewController inside Navigation Controller

I'm trying to have my app open to a specific ViewController that is deeply embedded in a Navigation Controller, but isn't the RootViewController. I have tried in the app delegate didFinishLaunchingWithOptions adding:

  self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
  let storyboard = UIStoryboard(name: "Main", bundle: nil)
  let initialViewController = storyboard.instantiateViewControllerWithIdentifier("NavViewController")
  self.window?.rootViewController = initialViewController
  self.window?.makeKeyAndVisible() 

But this goes to the root view controller. I tried changing this line to:

storyboard.instantiateViewControllerWithIdentifier("MainViewController")

and this does open to the correct viewcontroller but then there is no Navigation Bar on the top which is needed to navigate in the app.

Upvotes: 2

Views: 1390

Answers (2)

Neimsz
Neimsz

Reputation: 1554

Here is the Objective-C version to open a specific UIViewController deeply embedded inside a UINavigationController (which is not the rootController).

If we take @bdc example in comments and we want to open UIViewController D:

  • A : rootViewController
  • N : navigationController
  • B, C, D : UIViewControllers embedded in N
  • A -> N[B C D]

Set storyboardId in InterfaceBuilder for every UIViewController or UINavigationController in the hierarchy through D.

In AppDelegate, the code is straight forward. You just have to instantiate everything, and use your UINavigationController to push every UIViewController in your hierarchy.

// Instanciate from storyboard with identifiers

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
UINavigationController * N = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"N"];
UIViewController* B = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"B"];
UIViewController* C = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"C"];
UIViewController* D = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"D"];

// Set up the views hierarchy

self.window.rootViewController = N;
[N pushViewController:B animated:NO];
[N pushViewController:C animated:NO];
[N pushViewController:D animated:YES];
    
    
[self.window makeKeyAndVisible];

Hope this will help.

Upvotes: 0

Sohel L.
Sohel L.

Reputation: 9540

To access the rootViewController from the AppDelegate then here is the code:

let rootViewController = application.windows[0].rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let notificationVC = mainStoryboard.instantiateViewControllerWithIdentifier("identifier") as! NotificationVC
rootViewController.pushViewController(notificationVC, animated: false)

Now, it will have the navigationBar. Let me know, if you still face the issue. thanks.

Upvotes: 2

Related Questions