swiftunwind-segue

Is it possible to implement an exit segue without a Navigation Controller in Swift?


I'm new to Swift and working on a portfolio app. I've read that it's possible to add a button with an exit segue to go back a screen using a Navigation Controller. However, I'd like to implement this feature without using a Navigation Controller.

I've tried creating a button with an exit segue and connecting it to an IBAction, but the button remains inactive when clicked. All the tutorials I've found use a Navigation Controller to implement this feature, so I'm not sure if it's possible to do it without one.

My question is: is it possible to add a button with an exit segue and have it work as expected without using a Navigation Controller? If so, what steps do I need to follow to make it work?

Thanks for any help you can provide!

Here is the code I have in the SecondScreen Swift file:

import UIKit

class SecondScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func goBack(segue: UIStoryboardSegue) {
        // This method is called when the exit segue is performed.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goBack" {
            // This is where you would prepare any data to be passed back to the previous view controller.
        }
    }

}

The first screen has the follwoing code:

import UIKit

class FirstScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

When I have the all this setup, I drag and drop the button to the exit segue like I am told in all tutorials, but in the end it's still inactive

enter image description here


Solution

  • You didn't follow the tutorial directions correctly. Move goBack into FirstScreen and all will be well. This method is a kind of target telling the unwind segue where to go "back" to. (It doesn't have to have any content in its implementation; it just needs to be present in the desired destination.)