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)
        })
    }
}

1 Answer 1

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. 
        }
Sign up to request clarification or add additional context in comments.

8 Comments

Above code where I need to place? within second or this viewcontroller? @Mohammad S
@JamKu in the second VC where you present the third VC
Based on your solution I am getting crash
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is <UINavigationController: 0x13001ca00>.' @Mohammad S
are you calling it twice ? or did u replaced the code ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.