23

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)

pkamb
  • 33,281
  • 23
  • 160
  • 191
Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148

3 Answers3

35

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.

sch
  • 27,436
  • 3
  • 68
  • 83
7

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
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
dinesh sharma
  • 577
  • 10
  • 20
5

Instead of self.view.window use

[(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window]
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
cprcrack
  • 17,118
  • 7
  • 88
  • 91