4

I'm using .xib files in an app. And I want to have two view controllers normal way and from the third view controller, embed it in a navigation view controller. Here's an illustration of how I want it.

enter image description here

I know I can embed a navigation controller in a modally presented view controller like this.

let firstVC = FirstViewController(nibName: firstViewController, bundle: nil)
let navController = UINavigationController(rootViewController: firstVC)
presentViewController(navController, animated: true, completion: nil)

But if I embed it in a navigation controller and push it, the app crashes with the error Pushing the same view controller instance more than once is not supported.

let firstVC = FirstViewController(nibName: firstViewController, bundle: nil)
let navController = UINavigationController(rootViewController: firstVC)
navController.pushViewController(firstVC, animated: true)

Is it possible to do this at all? If so, can someone please explain how?

Thank you.

NOTE: Don't confuse the code snippets with the diagram above. The firstViewController in the code is not the first view controller in the diagram.

3
  • Are you referring firstViewController as the first image in above diagram? If so how are you navigating through first image to second? Through presenting? Commented Sep 9, 2014 at 12:38
  • Oh no no, the code snippet is not related to the diagram above. Sorry I forgot to mention that. Commented Sep 9, 2014 at 13:07
  • @Isuru How did you resolved your issue? Commented Nov 20, 2018 at 21:01

2 Answers 2

1

Suppose that FirstViewcontroller,Secondviewcontroller,Thirdviewcontroller are the three view controllers. then the transition from second to third view controller use the code given below.

Secondviewcontroller *second=[[Secondviewcontroller alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController: second];
[self presentViewController:nav animated:NO completion:nil];
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, this is how I already do the modal presentation. What I need is to push the view controller. Not present it modally.
you mean push to the third viewcontroller?
Exactly. Push to the third view controller and have it embedded inside a navigation controller.
If you want to push from second view controller, second view controller should have a navigation controller. unless you can't push from that view controller.
1
  1. use the first view controller as the root view controller of the UINavigationController
  2. use the following method to present your next view controller

[self.navigationController pushViewController:vc animated: YES];

  1. in the viewWillAppear method, if you wanna hide the navigation bar, use

[self.navigationController setNavigationBarHidden:YES animated:animated];

If you wanna show the navigation bar, use

[self.navigationController setNavigationBarHidden:NO animated:animated];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.