-1

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

Ryne Ignelzy
  • 137
  • 1
  • 2
  • 13
  • "Unwind" is simply an automatic way of saying "pop" (if you arrived into this screen by push) or "dismiss" (if you arrived into this screen by present), possibly chaining them together as needed. If you didn't arrive into SecondScreen by a push or present, the notion of unwinding makes no sense. You didn't say anything about how you _did_ arrive into SecondScreen so no more can be said. – matt Apr 13 '23 at 13:18
  • arrived via Present Modally segue – Ryne Ignelzy Apr 13 '23 at 13:56
  • Show that please. Does your app have more code? – matt Apr 13 '23 at 13:57
  • there isn't really much code there. I created a segue from Button1 to screen 2, set it to Modally. Then created a button on screen two, added the above code and then draged the button to the exit icon in the main.storyboard, here is a repo https://github.com/RyneIgnelzy/segueTest – Ryne Ignelzy Apr 13 '23 at 14:28
  • Yup, now I see the problem. I suppose I could have seen it from your code, but seeing it in the project made it more obvious, so thanks for the project! Always a good way to communicate these storyboard-based issues. – matt Apr 13 '23 at 15:00

1 Answers1

1

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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Thank you! I knew there was something very simple that I was not getting. I actually thought that about this, but did not try it! – Ryne Ignelzy Apr 13 '23 at 20:15
  • Yeah, I regard this as a useful QA because it's such an easy mistake for a beginner to make. – matt Apr 14 '23 at 15:51