search by tags

for the user

adventures into the land of the command line

exit segues with xcode 7 and swift 2

While trying to learn how to do groovy transitions I came across this awesome blog post.

In it he explains view controller stacks really well:

“When we present a view controller in iOS with a segue it may seem like we’re replacing that screen, but actually we’re just adding a new screen onto a stack of screens in which only the top screen in that stack is visible.

But if we continue performing segues, we just end up adding more and more screens onto our stack of view controllers.

It may seem like we’re replacing the screens with each segue because all we see is the screen that’s on the top of the stack but with each segue the stack is growing larger and larger.

Eventually, if this is not handled the app will eventually crash because each screen requires memory to be allocated to it and there is always a finite amount of memory to use.

Instead of performing a regular segue to attempt to return to an earlier screen we should create an exit segue that allows us to unwind to an earlier screen in the stack.

Creating an exit segue is similar to a regular segue in that you start by selecting the object you want to trigger the segue, but instead of dragging to another view controller, you control-click and drag and release on the screen’s exit segue icon.”

Just with the segue, nothing will happen. We also need to add an IBAction to the view controller class that we want to exit to. The func name can be whatever you want.

@IBAction func exitToViewController (sender: UIStoryboardSegue) {

}

When you control-drag to the exit segue icon, a list of the IBActions you added will appear in a list, allowing you to choose where to exit.

Thank’s Matthew Sanders!