Storyboard 属性 是 UIViewController 的属性吗?

Is the storyboard property an attribute of UIViewController?

另一个 post 提出了这个问题,我无法让他们解释他们的意思。我认为情节提要是 UIViewController 的一个属性,因为 Apple 的参考文档在读取 "Interacting with Storyboards and Segues" 的区域中说了 var 情节提要。 Apple UIViewController reference doc 其中一条评论是 "The storyboard isn't a property or an attribute of either UIViewController",我希望他们能对此进行阐述。谁能帮忙,因为现在我对是否正确阅读或使用参考文档感到困惑。

最初的问题在这里:

在文档中,定义指出:

var storyboard: UIStoryboard?

The storyboard from which the view controller originated.

您认为 var storyboardUIViewController 的 属性 是正确的。您混淆的原因是您的心理模型假设因为 UIViewController 引用了 storyboard,它必须 拥有 它。 storyboard 只是对 加载 您的 UIViewController 故事板的引用。它应该更多地被视为一种自下而上的关系,而不是自上而下的关系。

在实践中,作为 UIViewController,一个常见的用例是要求您的 storyboard 实例化其他 UIViewController,然后我们可以呈现:

let viewControllerToPresent = storyboard?.instantiateViewController(withIdentifier: "presentedController")
present(viewControllerToPresent, animated: true, completion: nil)

UIViewControllerclass绝对有一个storyboard property.

你的另一个问题是询问初始视图控制器的实例化和初始化。在这个问题的上下文中,我认为的意思是 UIViewController class 在实例化之前 没有以某种方式知道的故事板属性 这让它可以从情节提要中实例化自己。

相反,UIStoryboard 在创建 UIViewController 实例后设置 storyboard 属性。这是 segue 工作所必需的。

请注意,storyboard 属性 是可选的,如果实例不是由情节提要创建的,它将没有值。