2
votes

I'm starting to learn iOS development and am attempting to use UINavigationController as my window's root view. All is working well but I need some advice on how to structure my app. I read the docs and some questions on here too.

Since the navigation controller is managing all my other content view controllers, then all of these view controllers need a way to send messages to the navigation controller. Right? So, I've thought about making a singleton navigation controller that any other view controller can call on to push new view controllers on it. Or, if each view controller has a reference to the navigation controller then they can push/pop easily as well. This is the part I'm not sure about.

Also, for having buttons and actions I have been setting the target as the navigation controller and from there it can handle it correctly and push or pop on it's own. I did subclass UINavigationController for this. And I have my view controllers as references in it. However I ran into an issue where my UITableViewController was handling a selection of a row and I need to push a new view controller on top, but how do I get the reference to the navigation controller?

I hope that makes sense, and any advice on how to structure this would be very appreciated.

Thanks!

2

2 Answers

0
votes

I think you're overthinking this. View controllers have a navigationController property built in allowing you to reference the navigation controller. That being said, pushing to a new view controller from within a view controller that is embedded in a navigation controller is as easy as:

UIViewController *myNewViewController = [[UIViewController alloc] init];

[self.navigationController pushViewController:myNewViewController animated:YES];
0
votes

According to your requirement you need to write like this :-

     UIViewController *myNewViewController = 
    [[[UIViewController alloc] 
    initWithNibName:@"yourNibName"] 
   bundle:nil]];

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

This will push one controller on the bottom of the stack.