Search code examples
iosios7uiviewcontrolleruistoryboarduistoryboardsegue

Nested UIViewController is returned:'ing out too far


I'm missing something fundamental I think. I have a story board that looks like:

enter image description here

In short, I have 3 of my own controllers, embedded individually in UINavigationControllers.

I'll refer to those (my own) controllers as 1, 2, and 3, from left to right. I annotated the picture to show how I hooked things up. I created a segue between the controller 1 and 2, by connecting its disclosure indicator to the navigation controller containing #2. I created the segue between 2 and 3, by just connecting the controllers and naming it. I invoke that segue programmatically using

[self performSegueWithIdentifier:@"OpenValvesSelector" sender:self];

For both controllers 2 and 3, I connected both cancel and save navigation items with the exit item of the containing controller.

The problem is that when I have 3 opened, and select either the Cancel of Save item, it closes not only that view, but 2 as well, returning me to controller 1. I don't want that. I'm trying to use 3 to edit some content from 2. How do I get it to just returned: to 2, instead of 1?

On a broader note, I'm not entirely convinced I've done the right thing by having UINavigationControllers there. I tried doing with out the UIViewController that is containing 3, but then there was no where to edit the title/button items for that scene. I've read the Apple Docs on View Controller Programming Guide for iOS, but this level of detail doesn't seem to be there.


Solution

  • When you use an unwind segue, the way the runtime decides how far "back" to unwind is by looking to see, in the first instance, where the corresponding unwind method is implemented. Thus, if you are in view controller 3 and you want to unwind to view controller 2, you need to implement the unwind method in view controller 2.

    (Don't worry: you will still be able to unwind from view controller 2 to view controller 1.)