Is there any way to create and use an unwind segue programmatically?
<p>I have a series of three view controllers, the first two of which are wrapped in a UINavigationController, and are managed by the default <code>Main.storyboard</code> file. Let's call them <code>A</code> and <code>B</code>, respectively. I have more view controllers throughout the file, each instantiated and managed separately, though I am working on moving them into all-code solutions. The aforementioned third view controller I have already in code (<code>C</code>) is one that at various times is instantiated and presented by <code>B</code>.</p>

<p><code>A</code> and <code>B</code> have a normal segue relationship setup in Interface Builder and called via code. I do the same with <code>C</code> from <code>B</code>, via code (instantiate and <code>presentViewController(_:)</code>). There are a few cases where an action in <code>C</code> invalidates <code>B</code>'s raison d'être, and thus I must dismiss both.</p>

<p>So far, I have been calling <code>dismissViewControllerAnimated(_:)</code> from <code>C</code>, and then checking in <code>B</code>'s <code>viewDidAppear(_:)</code> whether it should be dismissed, and dismissing it the same way if so. During this process, the user is thrown back though the VC hierarchy, watching as empty views fly back to whence they came, leaving time for them to experiment with controls that no longer work, and may crash the app. This is a rather disconcerting user experience that I wish to do away with. I could simply disable everything, but that's a lot of controls that I'd rather not mess with if I can avoid it...</p>

<p>I understand that IB supports "unwind segues," which can dismiss an entire VC hierarchy simultaneously, though those only seem to handle view controllers in the storyboard. Is there any way to get this behavior (dismiss everything to <code>A</code>) programmatically, without having to revert much of the work I've already done, considering that part of my hierarchy is contained in a <code>UINavigationController</code>?</p>

<hr>

<p>UPDATE:</p>

<p>So, I got the dismissal to work properly, by passing a reference to the presenting view, and calling its dismiss segue before leaving. That approach came with its own issues and navigation controller weirdness, but with some tweaking it might be usable.</p>

<p>Honestly, it would be much easier to remove the feature entirely at this point, as it's mostly a convenience.</p>

<p>For the sake of science, I'm going to keep at it until I decide ether way, and answer back here for anyone googling this way.</p>

<hr>

<p>UPDATE:</p>

<p>Ew... It seems this code was older than I thought. I actually have two navigation controllers, to support a custom modal animation into and out of <code>B</code>, with a custom unwind segue there (there you go). In order to get the animation I want, I may as well toss <code>C</code> into the storyboard and make a custom unwind segue.</p>

<p>If I don't care about animation, simply disabling animation on the custom unwind got both <code>B</code> and <code>C</code> to vanish promptly, and together. Trouble is, it's a bit jolting for my taste...</p>

<p><a href="https://stackoverflow.com/users/1630618/vacawama">vacawama</a>'s suggestion actually makes a lot of sense, and serves me right for not checking the documentation! <code>UIViewController</code> already keeps a reference to its <code>presentingViewController</code>. Going back by two is a pinch, simply climbing back that hierarchy and dismissing the one I want. Works like a charm, without animation doesn't happen at all...</p>

<p>Gonna post an answer here soon with what I've found thus far.</p>