3

I have a navigation tableview controller Calculator and that goes to AddActiveIngredients which is where I add everything in for the calculator.

I have the segue method backButton in Calculator and in AddActiveIngredients I have click dragged the segue from the controller to exit and selected the segue in the Calculator class.

enter image description here

The segue identifier is also backButton but it's not doing anything.

I've tried this code to try and trigger the segue manually but that's not working. Am I missing something so simple?

override func didMoveToParentViewController(parent: UIViewController?) {
    if (!(parent?.isEqual(self.parentViewController) ?? false)) {
        print("Back Button Pressed!")

        self.performSegueWithIdentifier("backButton", sender: self)
    }
}
Callum
  • 1,136
  • 3
  • 17
  • 43

3 Answers3

2

It sounds like you just want to remove the AddActiveIngredients from the stack. If so, you just need to call a function to dismiss it.

func dismissVC() {
  dismissViewControllerAnimated(true, completion: nil)
}

You can put that in a button tap or something else. It'll work with Show and Modal segues. Unwinding may be overkill for what you're doing.

Hope it helps!

D. Greg
  • 991
  • 1
  • 9
  • 21
1

To use the Exit icon you need not create a segue at all.

All you have to do is add a method in the ViewController to which you want to unwind to and add a method with the signature:

@IBAction func myUnwindAction(segue: UIStoryboardSegue) {
    // do stuff
}

Remember, you have to add this method in the target ViewController

When you Control-Drag from a button to the Exit icon, this method will now show up. When you now click the button, the current ViewController will be popped and the action method in the target will be called.

Hope this helps.

Thomas Krajacic
  • 2,488
  • 1
  • 21
  • 25
  • 1
    I'm using the navigation controller so it adds a back button in. There's no button to get the segue action from. – Callum Mar 10 '16 at 12:33
  • So if I understand you correctly, you want to trigger the same action that the "back" button has? Then you'd just have to call dismissViewController like @d-greg answered above. – Thomas Krajacic Mar 11 '16 at 11:49
  • He said "You can put that in a button tap or something else" but the back button is added in with the navigation controller so there's no button to bind it to – Callum Mar 11 '16 at 12:04
  • Mhm, apologies, I seem to not understand exactly what you are trying to accomplish. So you do not want to use the provided back button? If you want to use your own action to go back, you have to hook it up to some UI element (usually your own button) to trigger it. Maybe you can provide a little bit more information as to what exactly you are trying to accomplish. I am sure we can figure it out then. – Thomas Krajacic Mar 12 '16 at 12:35
  • @ThomasKrajacic What if I want to use the back button that the UINavigationController provides, and I want to use my code to run when the back button is tapped, the same way that a bar button item when tapped will trigger code in the UIViewController prepare(for:sender:) instance method? – daniel Sep 29 '22 at 03:49
1

with Swift 5 you can use Dismiss function

@IBAction func dismissViewController(_ sender: Any) {
        //Go back to previous controller
        dismiss(animated: true, completion: nil)
    }
swiftBoy
  • 35,607
  • 26
  • 136
  • 135