OGeek|极客世界-中国程序员成长平台

标题: iphone - 初始化 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:27
标题: iphone - 初始化 View Controller

When a view controller is first instantiated, it usually creates or loads objects it needs through its lifetime. It should not create views or objects associated with displaying content. It should focus on data objects and objects needed to implement other critical behaviors.

以上来自iOS引用:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10

文档继续描述了使用 Storyboard 的 View 加载序列。

我的问题是:

1

由于 View Controller 将与包含 View 对象的 nib 文件相关联;而且它的“viewDidLoad”方法似乎是为在加载时配置 View 对象而设计的。那么文档的建议应该如何:

“不应创建与显示内容相关的 View 或对象”

被解释?

2

问题 1 是否与我们是否使用 Storyboard 相关?



Best Answer-推荐答案


不确定我的问题是否正确,但这是我的解释:

  1. init初始化和 View 创建是两个独立的步骤。假设我有一个 View Controller ,其表为 IBOutlet,它应该显示存储在核心数据中的配方列表。在我的初始化方法中,我会从 CoreData 获取数据并将其存储在数组或获取的结果 Controller 中。我不需要表格,因此我对 self.view 属性(如果还没有 View ,则调用 viewDidLoad )什么都不做。在 viewDidLoad 中,我调用 [tableView reloadData] 来重绘单元格,以便它们显示来自 Controller 初始化程序中创建的 Controller 的数据。

  2. 我不认为它是相关的,但 Storyboard应该只是你的 View Controller 的脚手架,用单个文件替换单独的 nib。

关于iphone - 初始化 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10062269/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4