3
votes

I have an application with a UINavigationController that contains a root UIViewController subclass (PagingViewController) which manages and acts as the delegate for a horizontally paging UIScrollView. On each of the pages of this UIScrollView, there is a vertically scrolling UITableView, each managed by its own UIViewController subclass (PageViewController). It looks something like this...

UINavigationController
  PagingViewController (root VC of navigation controller)
    PageViewController (instance 1)
    PageViewController (instance 2)
    PageViewController (instance 3)

  HowToPushThisViewController (this is what I'd like to push onto the current root, PagingViewController, from one of its sub view controllers)

What I'd like to do is, in the didSelectRowAtIndexPath: method of the PageViewController (UITableView delegate), push a new UIViewController onto the UINavigationController (on top of the PagingViewController).

The issue is that the navigationController property of the PageViewController is nil, so I can't push it there. I assume this is nil because it is not DIRECTLY on the UINavigationController's stack, but contained by the PagingViewController which is on the stack. I tried setting the navigationController property of each PageViewController before adding it to the PagingScrollView, but it seems to be read only (How does the UINavigationController set itself?).

I feel like the nested UIViewControllers is sound architecture (each one is needed to manage and act as delegate for its views), and it's reasonable to want to push navigation from a sub controller, so I'm not sure the best practice for accessing and pushing from the nested controller. Hopefully there is something simple I'm overlooking to access and push onto the root naviagtion controller.

1

1 Answers

2
votes

The logical thing to do would be to set the page view controller's parentViewController property but alas, it is read-only too. Apple's does not encourage custom hierarchies of view controllers (besides the supported hierarchies with tab bar, navigation and split view controllers).

In your case, I would define a custom parentVC propertyy on the PageViewController class that the PagingViewController sets to itself when it instantiates its children. The PageViewControllers can then get to the nav controller via

self.parentVC.navigationController