UIViewController.View.Window is null in ViewDidLoad method
11,153
Solution 1
According to the documentation of UIView, the window
property is nil
if the view has not yet been added to a window which is the case when viewDidLoad
is called.
Solution 2
Instead of self.view.window
use
[(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window]
Solution 3
self.view.window
will be available in viewDidAppear:
override func viewDidAppear(_ animated: Bool) {
print(self.view.window)
let vc = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController
self.view.window?.rootViewController = vc
}
Author by
cheesus
StackOverflow please stop firing all the Moderators. Please fire Sara Chipps instead.
Updated on July 03, 2022Comments
-
cheesus almost 2 years
Regardless on which controller type (
UIViewController
,UITableViewController
), the following line always yieldsnull
in theViewDidLoad
method:this.View.Window
Is this behavior normal, or am I doing something odd? What could lead to
UIViewController.View.Window
beingnull
?(I suppose this question concerns not only MonoTouch, but also 'normal' Objective-C Cocoa).
(MonoTouch 5.2.11, Xcode 4.2.1 4D502)
-
Alex Ryan over 9 yearsWhat method is called after the view is added to a window?
-
Renfei Song about 9 years@AlexRyan You can use
-viewDidAppear
. -
Pedro Paulo Amorim over 4 yearsIt's a problem if you are using
UIScene
.