MVC 中的 ViewController、XCode 中的 ViewController
一个简单的问题,
我最近一直在涉足 XCode,并试图理解视图控制器,虽然我了解了它的本质,但我没有看到的一件事是视图控制器类对象的实例化位置。它本质上是一个类,因此必须实例化一个对象才能向其发送消息。
这让我有点摸不着头脑。
非常感谢!
a quick question,
I have been dabbling with XCode off late and am trying to understand the View Controller, while I get the nitty gritty of it, one thing I fail to see is where the View Controller class object is instantiated. It is, in essence a class and hence has to have an object instantiated to be able to send messages to it.
It's kind of left me scratching my head.
Thanks much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当需要显示时就会实例化它。由你。
例如,如果您想在按下按钮时在导航堆栈上显示新视图?
我相信在大多数情况下,这样做通常比在内存中保存实例更好,以防止过多的内存使用。
现在(假设 iOS5 已经结束 NDA,大部分内容已在今天公布),您可以使用 XCode 中的 Storyboarding 来为您处理所有这些事情。
It is instantiated whenever it needs to be displayed. By you.
For example, if you wanted to display a new view on the navigation stack on the press of a button?
I believe it is generally better to do this instead of holding an instance in memory in most situations, to prevent too much memory usage.
Now (assuming iOS5 is out of NDA now that most of it has been announced today), you can use Storyboarding in XCode that will handle all this for you.
视图控制器在其 init 函数指定的初始化程序中实例化,如下所示:
正如您可能在一些 Apple 示例或您浏览过的任何其他源代码中注意到的那样,您可能已经看到了类似于以下的代码行:
This is where/当视图控制器被实例化时。您会注意到视图控制器有一个名为 view 的 UIView 类型的成员对象,它被添加到该视图所属的窗口或视图中。
创建视图控制器是为了处理与该视图相关的消息。一切都在此处详细说明。
The view controller is instantiated in it's init function designated initializer being the following:
As you will perhaps have noted in some of Apples examples or any other source code you've looked through you might have seen a line of code similar to
This is where/when the view controller gets instantiated. You'll notice that a view controller has a member object of type UIView called view it is this that gets added to the window or view that this view is to be apart of.
The view controller is created to handle messages pertaining to this view. It's all spelled out here.