Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift back button segue

Tags:

ios

swift

iphone

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)
    }
}
like image 664
Callum Avatar asked Mar 24 '26 22:03

Callum


2 Answers

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!

like image 101
D. Greg Avatar answered Mar 27 '26 11:03

D. Greg


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.

like image 42
Thomas Krajacic Avatar answered Mar 27 '26 12:03

Thomas Krajacic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!