1

My Scenario, I have three ViewControllers, those ViewControllers First, Second, Third. Here, First ViewController UIButton click to present model second ViewController and then Second ViewController UIButton click to present model Third ViewController. I need to Implement the Second ViewController button click to dismiss current ViewController after presenting Third ViewController because once I close Third ViewController I need to show First ViewController. How to Achieve this?

NOTE: I am having NavigationController for Third ViewController also I am using Storyboard.

Here's my code:

@IBAction func ClickThirdVC(_ sender: Any) {
    if let VC_Third = self.storyboard?.instantiateViewController(withIdentifier: "thirdviewcontroller") as? ThirdvViewController {
        weak var pvc = self.presentingViewController
        VC_B.modalTransitionStyle = .crossDissolve
        let navController = UINavigationController(rootViewController: VC_B)
        self.dismiss(animated: true, completion: {
            pvc?.present(navController, animated: true, completion: nil)
        })
    }
}
Harsh Pipaliya
  • 2,260
  • 1
  • 14
  • 30
Jam Ku
  • 345
  • 1
  • 2
  • 17

1 Answers1

1

You can try to dismiss self after presenting the third UIViewController, in the completion handler of the present function .

secondVC?.present(navController, animated: true) {
            self.dismiss(animated: false, completion: nil) //dismiss self after presenting the third. 
        }
Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • Above code where I need to place? within second or this viewcontroller? @Mohammad S – Jam Ku Jun 24 '19 at 07:18
  • @JamKu in the second VC where you present the third VC – Mohmmad S Jun 24 '19 at 07:18
  • Based on your solution I am getting crash – Jam Ku Jun 24 '19 at 07:23
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is .' @Mohammad S – Jam Ku Jun 24 '19 at 07:28
  • are you calling it twice ? or did u replaced the code ? – Mohmmad S Jun 24 '19 at 07:29
  • if let VC_Third = self.storyboard?.instantiateViewController(withIdentifier: “thirdviewcontroller”) as? ThirdViewController { let navController = UINavigationController(rootViewController: VC_Third) VC_Third.present(navController, animated: true) { self.dismiss(animated: false, completion: nil) //dismiss self after presenting the third. } } @Mohammad S – Jam Ku Jun 24 '19 at 07:30
  • you're using it wrong you need to call `self.present` not `VC_Third.present` – Mohmmad S Jun 24 '19 at 07:31
  • Its not working. Still some problem. Third viewcontroller Navigation bar showing without color and also third viewcontroller dismiss immidiatly after presenting. @Mohmmad S – Jam Ku Jun 24 '19 at 07:34