-5

My scenario, I am trying to create multiple present ViewController. Here, presenting new ViewController after I need to dismiss previous ViewController.

ViewController A (RootViewController) next button click to presenting ViewController B then View Controller B next button click to present ViewController C. Now, If I close ViewController C need to show ViewController A.

example

channu
  • 216
  • 2
  • 9
Jam Ku
  • 245
  • 2
  • 10
  • Might I suggest you take a look at https://www.youtube.com/watch?v=ULd2v4mHyQ4 – Bram Jul 04 '19 at 12:30
  • @Craz1k0ek there is nothing clear idea also I am looking for present model view controller – Jam Ku Jul 04 '19 at 12:32
  • I did not understand your question. When you dismiss C, you want to show A ? Or you want to dismiss the current VC before presenting next VC ? – Teja Nandamuri Jul 04 '19 at 12:35
  • @TejaNandamuri when I dismiss C, I need to show A. – Jam Ku Jul 04 '19 at 12:36
  • `self.presentingViewController?. presentingViewController?.dismiss...` – Prashant Tukadiya Jul 04 '19 at 12:42
  • 1
    Better to use a navigation controller, and push & pop view controllers from it, instead of directly displaying and dismissing. – Asad Ali Choudhry Jul 04 '19 at 12:42
  • see this for help : https://stackoverflow.com/questions/33520899/single-function-to-dismiss-all-open-view-controllers – Anbu.Karthik Jul 04 '19 at 12:43
  • @AsadAliChoudhry My design present ViewController. I would like to know how to do that – Jam Ku Jul 04 '19 at 12:43
  • you need to have a reference to your viewController B, and you can only show A by dismissing both B and C. In your C controller, dismiss both C and B or just the dismiss the initial Vc. – Teja Nandamuri Jul 04 '19 at 12:47
  • @TejaNandamuri. Yes exactly I am trying to get. Please post some code – Jam Ku Jul 04 '19 at 12:50
  • please refer the link posted by Anbu.Kartik – Teja Nandamuri Jul 04 '19 at 12:51
  • @TejaNandamuri. I am using this UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil). its working fine for first time but If I close Viewcontroller A. I am showing tabbarviewcontroller again tab bar item one click to show this VC A to B and C, now if I close it. it is showing slightly Tabbar controller first time – Jam Ku Jul 04 '19 at 12:56
  • Possible duplicate of [Pop to root view controller from modal](https://stackoverflow.com/questions/39830955/pop-to-root-view-controller-from-modal) – 10623169 Jul 04 '19 at 13:13
  • @zb1995. first read my question and then make it duplicate. I am asking about present viewcontroller. You marked with navigation controller. Please mark it genuine don't do for credits. also why you gave downvoted for this. – Jam Ku Jul 04 '19 at 13:16

1 Answers1

1

Here is how you can proceed,

class VCA: UIViewController {
    @IBAction func onTapNextButton(_ sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "VCB") as? VCB {
            self.present(controller, animated: true, completion: nil)
        }
    }
}

Since VCC is embedded in a UINavigationController, you need to present UINavigationController instead of VCC.

For this subclass UINavigationController and set it as class of UINavigationController in the storyboard.

class VCB: UIViewController {
    @IBAction func onTapNextButton(_ sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "NavVC") {
            self.dismiss(animated: false, completion: nil)
            self.presentingViewController?.present(controller, animated: true, completion: nil)
        }
    }
}

class NavVC: UINavigationController {}

class VCC: UIViewController {
    @IBAction func onTapCloseButton(_ sender: UIButton) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
}
PGDev
  • 20,976
  • 5
  • 29
  • 68