Search code examples
iosswiftxcodesegueunwind-segue

With storyboards, how can I perform a reverse segue to return to the previous screen?


Currently, I am working on a project which requires multiple screens. When I transition from one screen to another, the default transition when pressing a button is the next screen rising upwards to cover the previous screen. Naturally, when I press a "back" button, I would like the reversal of this segue, where the screen falls downwards, reveling the previous screen. I have tried to devise some code for this to happen:

Code Code

Whenever I link a "back" button to this action segue, and press the button, It will always lead to the very first screen of the game, rather than the previous screen. Currently, I am left with the back buttons leading towards the previous screen, creating a very unnatural feel to the game, as it appears that all the screens are layering on top of one another. Is there a way to fix this?

Thanks in advance.


Solution

  • Your code need to be changed as:

    @IBAction func unwindSegue(segue: UIStoryboardSegue) { }
    

    Use segue instead of sender in your action.

    If you are presenting a ViewController you need to dismiss the current ViewController

    self.dismiss(animated: true, completion: nil)
    

    Or if you're in a navigation controller you can "pop" it:

    self.navigationController?.popViewController(animated: true)