Question

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;
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top