0

I have my first NavigationController set up with root viewcontroller - PhotoViewController, It's a collectionView with grid of images. When image is selected I wan't to present a second navigationController with root controller - DetailViewControler.

My first navigationController is set up in appDelegate. I really don't understand how and where should I create the second. I'm not using IB. At the moment I have this in my DetailViewcontroller viewDidLoad:

    DetailViewController *detailView = [[DetailViewController alloc] init];
self.detailController = [[UINavigationController alloc] initWithRootViewController:detailView];

But when i'm trying to push a new controller, nothing happens. I guess that my code is in wrong place.

When I try to present second controller modally from my PhotoViewController, I have an error(trying to present nil viewController).

2
  • why do you want to present a second navigation controller? Why not present the detail view after selecting an image so that pressing back will get you back to the PhotoViewController? Commented Apr 4, 2014 at 19:55
  • I'm building this app purely for learning purposes. I just wan't to learn how it's done. Commented Apr 4, 2014 at 19:58

2 Answers 2

2

The common idea is to have a single navigation VC that contains -- and lets you navigate between -- other VCs. In the situation you describe, you wouldn't create another navigation VC. Instead, create just the detail VC and push that onto the existing navigation VC...

// assuming self is a view controller contained by a navigation controller
self.detailController = [[DetailViewController alloc] init];
[self.navigationController pushViewController:self.detailController animated:YES];
Sign up to request clarification or add additional context in comments.

3 Comments

So multiple navigationControllers is a bad practise? I'm presenting detailVC modally using [self.navigationController presentViewController:detailView animated:YES completion:nil]; but when I try to push something onto detailVC, example, commentVC, it doesn't work - CommentViewController *commentView = [[CommentViewController alloc] init]; [self.navigationController pushViewController:commentView animated:YES]; Is it even possible to push something on after view is presented modally, in same controller? I've done this in past using IB and multiple navigationVC.
Not always a bad practice. If you have a master view, and want to present a detail view, then push onto the master's existing nav vc is good. If you're starting a brand new flow, then a "modal" push of a brand new nav vc (with a new root) is okay. But that second case is more rare, and your situation sounds more like master-detail.
Thanks, my case was the second one. Forgot to mention that in my question.
0

Use a parent view controller as your initial view controller and add the navigation controllers as children, then transition between them. See this blogpost that I wrote: http://sketchytech.blogspot.co.uk/2012/09/container-view-controllers-parent-view.html

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.