【问题标题】:UIViewController.View.Window is null in ViewDidLoad methodUIViewController.View.Window 在 ViewDidLoad 方法中为空
【发布时间】:2012-04-23 09:47:43
【问题描述】:

无论是哪种控制器类型(UIViewControllerUITableViewController),以下行始终在 ViewDidLoad 方法中生成 null

this.View.Window

这种行为是正常的,还是我在做一些奇怪的事情?什么会导致UIViewController.View.Window 成为null

(我想这个问题不仅涉及 MonoTouch,还涉及“正常”的 Objective-C Cocoa)。

(MonoTouch 5.2.11,Xcode 4.2.1 4D502)

【问题讨论】:

    标签: iphone ios cocoa-touch xamarin.ios


    【解决方案1】:

    根据UIView 的文档,window 属性是nil,如果视图尚未添加到窗口中,则在调用viewDidLoad 时就是这种情况。

    【讨论】:

    • 视图添加到窗口后调用什么方法?
    • @AlexRyan 你可以使用-viewDidAppear
    【解决方案2】:

    而不是self.view.window 使用

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

    【讨论】:

    • 使用UIScene会有问题。
    【解决方案3】:

    self.view.window 将在 viewDidAppear 中可用:

    override func viewDidAppear(_ animated: Bool) {
        print(self.view.window)
        let vc = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController
        self.view.window?.rootViewController = vc
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多