Search code examples
swiftviewcontroller

how to present view controller from appdelegate or scenedelegate in swift5?


how to present view controller from appdelegate or scenedelegate in swift5 ? i tried this but didnt work :

self.window = UIWindow(frame: UIScreen.main.bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "profile")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()

Solution

  • The problem is you're initializing the window's root view controller as the instantiated storyboard view. You need to set your window's root view like so:

        self.window?.rootViewController = ProfileViewController()
    

    This sets it directly to the swift file, which should be a UIViewController of some sort, and doesn't use the Storyboard.