Faisal Syed
Faisal Syed

Reputation: 931

Navigation Bar not showing when presenting View?

In another file I'm creating an instance of my view controller from another storyboard and presenting it like so:

let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
self.presentViewController(viewController, animated: true, completion: nil)

In my storyboard I have the correct storyboard ID and embedded my view controller in a UINavigationController. I also set the initial view controller to the UINavigationController.

Why isn't it showing up?

Upvotes: 2

Views: 4333

Answers (3)

Martin
Martin

Reputation: 4765

For the ones who still use Objective-C:

UIStoryboard *sb = [UIStoryboard storyboardWithName: @"Storyboard" bundle: [NSBundle mainBundle]];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentViewController:nc animated:YES completion:nil];

Don't forget to thick the button:

enter image description here

Upvotes: 0

Fayza Nawaz
Fayza Nawaz

Reputation: 2306

Ok, so you're presenting the view controller (Please note, it's not navigation controller).

What you can do:

  1. Push the view controller

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    [self.navigationController pushViewController:viewController animated:YES];
    
  2. Present the navigation controller

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    
    let navigationController = UINavigationController(rootViewController: viewController)
    
    self.presentViewController(viewController, animated: true, completion: nil)
    

Upvotes: 2

Suresh.Chandgude
Suresh.Chandgude

Reputation: 306

create object of view controller then add navigation controller to it, and then present it :

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)

Upvotes: 8

Related Questions