Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

ios - dismiss two controllers Swift

I have this situation :

I have a first view controller , when tap on button in it I open in modal mode another view controller , in this view controller when I tap another button I open in modal view another view controller and in it there is a button and when I tap on it I want to go to first view controller without re-initialize it. How do I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the perfect situation for an unwind segue.

Put this in your first viewController (the one you want to return to):

@IBAction func backFromVC3(_ segue: UIStoryboardSegue) {
    print("We are back in VC1!")
}

Then in the Storyboard in your 3rd viewController, control-drag from your button to the exit icon at the top of the viewController and choose backFromVC3 from the pop-up.

Now, when the user presses the button in VC3, both VC3 and VC2 will be dismissed and you will return to VC1.


If you are not using Storyboards, you can dismiss the viewControllers with code. Here is code for a button's handler to dismiss two levels of viewController:

func doDismiss(_ sender: UIButton) {
    // Use presentingViewController twice to go back two levels and call
    // dismissViewController to dismiss both viewControllers.
    self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...