iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

mainViewController is nil #41

Closed hadiidbouk closed 6 years ago

hadiidbouk commented 6 years ago

Hello ,

I am showing the mainVC progmatically after performing some code in the appdelegate file,

The problem that i don't have any initial view controller in the storyboard.

the problem here self.contentView?.addSubview(self.mainViewController!.view)

the mainViewController is nil, i try to set it progmatically in my "MainVC" that extends PBRevealViewController but it seems that the loadView() in the PBRevealViewController start first.

hadiidbouk commented 6 years ago

I figure out that the problem is only happen when the segue is push not set.

iDevelopper commented 6 years ago

Hi, the code should be like this (if you don't want a right view controller, pass nil):

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let mainViewController = storyboard.instantiateViewController(withIdentifier: "MainNavController")
        let leftNavController = storyboard.instantiateViewController(withIdentifier: "LeftNavController")
        let rightViewController = storyboard.instantiateViewController(withIdentifier: "RightViewController")

        let revealViewController = PBRevealViewController(leftViewController: leftNavController, mainViewController: mainViewController, rightViewController: rightViewController)

        window?.rootViewController = revealViewController

        window?.makeKeyAndVisible()

        return true
    }