Reputation: 14465
Regardless on which controller type (UIViewController
, UITableViewController
), the following line always yields null
in the ViewDidLoad
method:
this.View.Window
Is this behavior normal, or am I doing something odd? What could lead to UIViewController.View.Window
being null
?
(I suppose this question concerns not only MonoTouch, but also 'normal' Objective-C Cocoa).
(MonoTouch 5.2.11, Xcode 4.2.1 4D502)
Upvotes: 23
Views: 15088
Reputation: 30729
If using UIScene
you can get the window from viewWillAppear
like this:
func windowByParent(vc: UIViewController) -> UIWindow? {
var window: UIWindow? = nil
var parent: UIViewController? = vc
while parent != nil && window == nil {
window = parent?.view.window
parent = parent?.parent
}
return window
}
Upvotes: 0
Reputation: 617
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
}
Upvotes: 9
Reputation: 19169
Instead of self.view.window
use
[(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window]
Upvotes: 5