11
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.

6
  • mainTabController.present(mainTabController, .... Your error is right here. You're attempting to have mainTabController present itself.
    – Kyle H
    Aug 30, 2018 at 21:14
  • Thanks @KyleH now I get the error Cannot convert value of type 'MainTabController.Type' to expected argument type 'UIViewController'.
    – BPDESILVA
    Aug 30, 2018 at 21:18
  • @MRDR in this case you probably have the M capitalized, which means you'd be handing the class type over as an argument, when what you want is an instance of the class handed over as the argument.
    – TomQDRS
    Aug 30, 2018 at 21:31
  • @TomQDRS Attempt to present <Cake.MainTabController: 0x7fba608aca00> on <Cake.MainTabController: 0x7fba6081a600> whose view is not in the window hierarchy!
    – BPDESILVA
    Aug 30, 2018 at 21:33
  • What class is this code written in by the way? From where are you calling it? If you are inside a View Controller, please take a look at @Carpsen90's answer, he tells you correctly what you'd need to change.
    – TomQDRS
    Aug 30, 2018 at 21:36

2 Answers 2

7

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()
    }
}
3
  • Hi I'm on the AppDelegate file anything I can do there ?
    – BPDESILVA
    Aug 30, 2018 at 21:30
  • @MRDR The AppDelegate isn't it's own View Controller, so it can't "present" a view controller. If you want to programmatically instantiate your Controller from the delegate, please refer to this answer: stackoverflow.com/a/26757245/4442731
    – TomQDRS
    Aug 30, 2018 at 21:41
  • @Carpsen90 Just figure it myself too. Thank you both for the support much appreciated !
    – BPDESILVA
    Aug 30, 2018 at 21:55
2

Remove the controller before present, so instead of

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

use just:

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.