Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift?

My Scenario, In my project I am maintaining three ViewController (Main, VC1 and VC2). In main ViewController I am maintaining UIButton, This button click to VC1 presenting model view controller. Again, VC1 I am maintain UIButton with action click to present model to VC2. After VC2 presenting I need to dismiss VC1.

//  presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController

self.dismissViewControllerAnimated(false) {
      // go back to MainMenuView as the eyes of the user
      presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
like image 596
jackios Avatar asked Jan 21 '26 03:01

jackios


1 Answers

Try this in VC2's close button action

var vc = self.navigationController?.presentingViewController
while vc?.presentingViewController != nil {
    vc = vc?.presentingViewController
}
vc?.dismiss(animated: true, completion: nil)
like image 193
RajeshKumar R Avatar answered Jan 22 '26 18:01

RajeshKumar R



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!