4

I have the following setup:

SplitViewController as rootController.

Detail part is first

ViewController with View > ContainerView (later, View will have ImageView, but that is not the issue here).

The ContainerView has segue (embed) to another view controller (NavigationController).

This is graphically represented in IB as:

Layout in IB

Now the thing is I want to access the NavigationController from rootController (eg SplitViewController). I was unable to navigate down the hierarchy of "subViews" and so.

Is there some convenient way to get hold of the NavigationController?

Without the ViewController (together with ContainerView), I was able to access it like:

UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
// now i have the controller, i can delegate to it or use it in any other way:
splitViewController.delegate = (id) navigationController.topViewController;
Ondrej Skalicka
  • 3,046
  • 9
  • 32
  • 53

1 Answers1

1

When you add the NavigationController, you could pass it upwards (delegate methods) to your subclass of UISplitViewController and store a reference there.

You could add a @property (MySplitViewController *) delegate; your MyContentView and set it to the splitviewcontroller. When the segue is fired, you could then do the following:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowNavigationController"])
    {
        UINavigationController *controller = segue.destinationViewController;
        [self.delegate setNavigationController:controller];
    }
}

Edit: If you want to stick to your code, you could do something like this:

UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
UIViewController *container = [splitViewController.viewControllers lastObject];
UINavigationController *navigationController = [container.childViewControllers lastObject];
splitViewController.delegate = (id) navigationController.topViewController;

In that case you should really include some error handling.

NoilPaw
  • 714
  • 5
  • 11
  • I don't have access to NavigationController to pass a delegate to id. My question is how to get hold of the NavigationController. Once I have its instance, I now how to proceed. – Ondrej Skalicka Feb 22 '13 at 12:43
  • @Savannah: If i understand correctly, you could use the segue, which adds the navigationcontroller to bind it to your splitviewcontroller. – NoilPaw Feb 22 '13 at 12:50
  • @Savannah: Please have a look at my edited answer. If i should add more detail, please let me know. – NoilPaw Feb 22 '13 at 12:57
  • @Savannah I added another way of setting the delegate. – NoilPaw Feb 22 '13 at 15:27
  • 1
    well, after a bit of tweaking, your Segue solution works wonders! Thanks a lot! – Ondrej Skalicka Feb 26 '13 at 13:11