I need to present view controller from the AppDelegate
, so I wrote the following code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let authViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as ViewController
if let keyWindow = UIApplication.sharedApplication().keyWindow {
keyWindow.rootViewController = authViewController
}
Unfortunately, window
and keyWindow
are both nil
. Why?
AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.
Overview. Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.
You need to create the window yourself in AppDelegate
if not using the main interface option:
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
Swift 3.0+
self.window = UIWindow(frame: UIScreen.main.bounds)
Then call your code above, using window
.
Finally, call makeKeyAndVisible()
on the window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With