在将它作为 subview 插入另一个 UIViewController
之前,我需要创建并加载一个 UIViewController
。
在 iOS 9 中,我可以执行 myController.loadViewIfNeeded()
。 iOS 8 的一个好的解决方法是什么?
似乎 myController.view.hidden = myController.view.hidden
有效,但它似乎是一个危险的 hack。
最佳答案
有很多方法可以强制 View Controller 加载它的 View 。事实上,最常见的方式(据我所知)是访问 View Controller 的 view
属性。
_ = viewController.view
我不会将此视为 hack。事实上,Apple 的文档指出,当它等于 nil
时访问 view 属性会导致加载请求的 View 。这意味着建议的解决方案是安全的,即使在未来的 iOS 版本中也能按预期工作。来自 Apple's documentation :
The view stored in this property represents the root view for the view controller's view hierarchy. The default value of this property is nil.
If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.
关于ios - loadViewIfNeeded 在 iOS 9 之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35239661/