Search code examples
iosxcodestoryboardsegue

How to Dismiss/Close Secondary Storyboard


I am creating a project in xcode and am using multiple storyboards for a few reasons, chief of which being able to separate multiple branching options that all contain a multitude of views. Using separate storyboards for some of this branched options allows me to keep my design organized and readable.

At any rate, I have created a button that, when pressed, has a show seque to a storyboard reference to my target storyboard. This works fine.

In the new storyboard, I have a navigation bar button item(exit) that presents a popover along the lines of "would you like to close this section and return to the main menu?". If the user clicks "yes", I would like to close/dismiss this storyboard to return to the Main.storyboard.

I can use another Storyboard Reference and a Show seque from the "yes" button, but I assume that this is not best practice and ends up creating a lot of cyclical resource usage.

What is the best way to achieve my intent in this situation?


Solution

  • I see you figured this out, however this question comes first when googling for dismiss storyboard reference.

    For the benefit of future readers, we need to put unwind segue action in the view controller where you want to return:

    @IBAction func unwind(_ segue: UIStoryboardSegue) {
        // function and argument names do not matter, the body could be empty
        print(#line, #function)
    }
    

    and connect the control (usually button) at the view controller from which you want to return to the exit of that view controller. The simplest way to do it is to Ctrl-drag the line from that control (button) to the last icon at the top of the view controller and choose Action Segue: unwind.