UIViewController.View.Window is null in ViewDidLoad method

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-22 17:20:54

问题


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)


回答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.




回答2:


Instead of self.view.window use

[(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window]



回答3:


you can call it like:- 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
    }


来源:https://stackoverflow.com/questions/10278204/uiviewcontroller-view-window-is-null-in-viewdidload-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!