ios - loadViewIfNeeded 在 iOS 9 之前

标签 ios swift uiviewcontroller

在将它作为 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/

相关文章:

ios - 通过仅覆盖某些数据 Swift 3 在 Firebase 数据库中设置值

ios - 错误域=NSCocoaErrorDomain 代码=512 "The operation couldn’ t be completed.The operation couldn't be completed.是一个目录

ios - 有没有办法在 Swift 中以编程方式获取所有应用的自动布局约束

swift - 找不到接受类型参数列表的 XCTAssertEqual 的重载 ([String : AnyObject], [String : AnyObject])

ios - 将数据从标签栏 Controller 传递到 swift 和 xcode 中的 View Controller

cocoa-touch - 不使用 transitionFromViewController 时我必须发送的 UIViewController 包含消息的正确顺序是什么?

ios - 添加基于 iOS 5 的框架

ios - Xamarin iOS : Check if Google fit app is installed on the device

iphone - 如何在不使用标签栏的情况下在 ViewController 之间切换?

ios - NavigationController pushViewController 不起作用