unwind segue not triggered. UIViewController presented modally and dismissed using swipe down gesture.

Here are the steps I took to implement the unwind segue.


  1. coded an unwind segue method in VC1.
  2. in storyboards, dragged vc2 to vc2 exit button and selected unwind method.
  3. unwind method prints a dictionary if it has received.

i want to go back to vc1 and vc1 should have some data that vc2 collected. so i expect unwind segue to do it.


but unwind segue doesn't get called. i checked with break points and nslog.

even prepareForSegueis not getting called.

what am i doing wrong?


Thanks

Accepted Reply

Where did you see that connecting the VC to exit button would trigger segue ?


What you could do is add a performSegue to unwind in viewWillDisappear of VC2

Replies

in storyboards, dragged vc2 to vc2 exit button and selected unwind method.


you dragged to exit button from which object ? Is it a button ?

If so, the button must not have its own IBAction defined.


I tested the set up, it works, prepare is called when I tap the button connected to exit (and unwindSegue).

I gave an identifier (UnwindToVC1) to the unwindSegue, so prepare in VC2 is:


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print("will unwind from VC2")
        if segue.identifier == "UnwindToVC1" {
            if let destVC = segue.destination as? VC1ViewController {
                print("I am UnwindToVC1")
               // To pass data : destVC.someProperty = someValue
            }
        }

    }


I can pass any data to VC1 through the prepare

unwind action is VC1 is


    @IBAction func unwindToVC1ViewController(_ sender: UIStoryboardSegue) {
        print("returned from unwind")
        print("I received", someValue)
    }

Vc2 file owner to vc2 exit button.

When do you expect to trigger the exit segue ?

When I dismiss it by swiping it down. I haven't put any button for dismissing. For time being I solved it by creating my data's instance in vc1 and updating it via its pointer in vc2, inside 'viewWillDisappear'. The object needs to persist in vc2 and for time being this does the trick.

Where did you see that connecting the VC to exit button would trigger segue ?


What you could do is add a performSegue to unwind in viewWillDisappear of VC2