Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application tried to present modal view controller on itself

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if userSignedInGlobal == "success"{
    if let mainTabController = storyboard.instantiateViewController(withIdentifier: "MainTabController") as?  MainTabController{
        mainTabController.present(mainTabController, animated: true, completion: nil)
    }
}

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is .'

I need to navigate to a page after Authenticating application with firebase to do so I use the above code after validating the authentication. How do I fix this an reference link or code explaining how to get there would suffice.

like image 712
BPDESILVA Avatar asked Aug 30 '18 21:08

BPDESILVA


2 Answers

If you are in a UIViewController, then change this line:

self.present(mainTabController, animated: true, completion: nil)

If you are in the Appdelegate then set your ViewController as the root view controller of the window property:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if userSignedInGlobal == "success"{
    if let mainTabController = storyboard.instantiateViewController(withIdentifier: "MainTabController") as?  MainTabController{
        window?.rootViewController = mainTabController
        window?.makeKeyAndVisible()
    }
}
like image 132
ielyamani Avatar answered Sep 27 '22 16:09

ielyamani


Remove the controller before present, so instead of

mainTabController.present(mainTabController, animated: true, completion: nil) 

use just:

present(mainTabController, animated: true, completion: nil)
like image 32
Valerika Avatar answered Sep 27 '22 16:09

Valerika