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

ielyamani
  • 17,807
  • 10
  • 55
  • 90
BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
  • `mainTabController.present(mainTabController, ...`. Your error is right here. You're attempting to have `mainTabController` present itself. – Kyle H Aug 30 '18 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 '18 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 '18 at 21:31
  • @TomQDRS Attempt to present on whose view is not in the window hierarchy! – BPDESILVA Aug 30 '18 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 '18 at 21:36
  • Sorry @TomQDRS I'm on AppDelegate , applicationWillEnterForeground method. Ok will do. Thank you ! – BPDESILVA Aug 30 '18 at 21:41

2 Answers2

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()
    }
}
Surender Kumar
  • 1,152
  • 16
  • 17
ielyamani
  • 17,807
  • 10
  • 55
  • 90
  • Hi I'm on the AppDelegate file anything I can do there ? – BPDESILVA Aug 30 '18 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: https://stackoverflow.com/a/26757245/4442731 – TomQDRS Aug 30 '18 at 21:41
  • @Carpsen90 Just figure it myself too. Thank you both for the support much appreciated ! – BPDESILVA Aug 30 '18 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)
Valerika
  • 366
  • 1
  • 3
  • 8