The name "View Controller" in iOS programming is a bit confusing as it includes both words "View" and "Controller" in the MVC model. Is "View Controller" actually just another name for Controller (as the view is actually mostly done by the Interface Builder). Why is it called a "View Controller"? Is it because this controller does all the view properties updating and event handling as well?
iOS编程中的“View Controller”这个名称有点令人困惑,因为它在MVC模型中包含了“View”和“Controller”两个词。 “View Controller”实际上只是Controller的另一个名称(因为视图实际上主要是由Interface Builder完成的)。为什么称它为“视图控制器”?是因为此控制器还执行所有视图属性更新和事件处理吗?
4 个解决方案
#1
3
Yes, a UIViewController
lives inside the "Controller" layer of MVC. But it is a special controller that controls and updates a view. View controllers can also handle events because they derive from UIResponder
.
是的,UIViewController位于MVC的“Controller”层内。但它是一个控制和更新视图的特殊控制器。视图控制器也可以处理事件,因为它们派生自UIResponder。
You can also create general controllers by subclassing NSObject
. For example an accounts controller that manages accounts etc. You actually find other controller classes in the iOS SDK, e.g. UIDocumentInteractionController
.
您还可以通过继承NSObject来创建通用控制器。例如,管理帐户等的帐户控制器。您实际上在iOS SDK中找到其他控制器类,例如UIDocumentInteractionController。
From the iOS View Controller Programming Guide:
从iOS View Controller编程指南:
For iOS applications, view controllers provide a vital link between an application’s data and its visual appearance. Understanding when and how to use view controllers is crucial to the design of iOS applications. View controllers are traditional controller objects in the Model-View-Controller design paradigm but they also do much more. In iOS applications, view controllers provide much of the logic needed to manage basic application behaviors. For example, view controllers manage the presentation and removal of content from the screen and they manage the reorientation of views in response to device orientation changes.
对于iOS应用程序,视图控制器提供应用程序数据与其可视外观之间的重要链接。了解何时以及如何使用视图控制器对于iOS应用程序的设计至关重要。视图控制器是模型 - 视图 - 控制器设计范例中的传统控制器对象,但它们也可以执行更多操作。在iOS应用程序中,视图控制器提供管理基本应用程序行为所需的大部分逻辑。例如,视图控制器管理屏幕上的内容的呈现和删除,并且它们响应于设备方向的改变来管理视图的重定向。
#2
2
"Controller" follows the thing being controller. If I were writing a controller for a chair I would call it a ChairController. Since a UIView is being controlled, it makes sense to call it a UIViewController right? The view itself is only responsible for display, but the logic of what to display is up to its controller.
“控制器”跟随控制器。如果我正在为椅子编写控制器,我会称之为ChairController。由于UIView正在被控制,因此将它称为UIViewController是有意义的吗?视图本身仅负责显示,但显示内容的逻辑取决于其控制器。
#3
1
From Apple docs - http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
来自Apple文档 - http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
The UIViewController class provides the fundamental view-management model for all iOS apps.
UIViewController类为所有iOS应用程序提供基本的视图管理模型。
Basically it is the container for your views and handles more logic than just the visual display that your view does.
基本上它是视图的容器,并且处理的逻辑不仅仅是视图所做的视觉显示。
Where necessary, a view controller:
必要时,视图控制器:
- resizes and lays out its views
调整并阐述其观点
- adjusts the contents of the views
调整视图的内容
- acts on behalf of the views when the user interacts with them
当用户与其交互时代表视图行事
#4
1
You should see a "View Controller" as a "Class that controles the view".
您应该将“视图控制器”视为“控制视图的类”。
This is a classic Model View Controller pattern (MVC).
这是一个经典的模型视图控制器模式(MVC)。
A UIViewController
has a view of type UIView
and you update this UIView
depending on your business objects (Model) for each particular controller.
UIViewController具有UIView类型的视图,您可以根据每个特定控制器的业务对象(Model)更新此UIView。
Also, events that occur on your UIView
, are sent back to your UIViewController
for handling them properly.
此外,UIView上发生的事件将被发送回您的UIViewController,以便正确处理它们。
#1
3
Yes, a UIViewController
lives inside the "Controller" layer of MVC. But it is a special controller that controls and updates a view. View controllers can also handle events because they derive from UIResponder
.
是的,UIViewController位于MVC的“Controller”层内。但它是一个控制和更新视图的特殊控制器。视图控制器也可以处理事件,因为它们派生自UIResponder。
You can also create general controllers by subclassing NSObject
. For example an accounts controller that manages accounts etc. You actually find other controller classes in the iOS SDK, e.g. UIDocumentInteractionController
.
您还可以通过继承NSObject来创建通用控制器。例如,管理帐户等的帐户控制器。您实际上在iOS SDK中找到其他控制器类,例如UIDocumentInteractionController。
From the iOS View Controller Programming Guide:
从iOS View Controller编程指南:
For iOS applications, view controllers provide a vital link between an application’s data and its visual appearance. Understanding when and how to use view controllers is crucial to the design of iOS applications. View controllers are traditional controller objects in the Model-View-Controller design paradigm but they also do much more. In iOS applications, view controllers provide much of the logic needed to manage basic application behaviors. For example, view controllers manage the presentation and removal of content from the screen and they manage the reorientation of views in response to device orientation changes.
对于iOS应用程序,视图控制器提供应用程序数据与其可视外观之间的重要链接。了解何时以及如何使用视图控制器对于iOS应用程序的设计至关重要。视图控制器是模型 - 视图 - 控制器设计范例中的传统控制器对象,但它们也可以执行更多操作。在iOS应用程序中,视图控制器提供管理基本应用程序行为所需的大部分逻辑。例如,视图控制器管理屏幕上的内容的呈现和删除,并且它们响应于设备方向的改变来管理视图的重定向。
#2
2
"Controller" follows the thing being controller. If I were writing a controller for a chair I would call it a ChairController. Since a UIView is being controlled, it makes sense to call it a UIViewController right? The view itself is only responsible for display, but the logic of what to display is up to its controller.
“控制器”跟随控制器。如果我正在为椅子编写控制器,我会称之为ChairController。由于UIView正在被控制,因此将它称为UIViewController是有意义的吗?视图本身仅负责显示,但显示内容的逻辑取决于其控制器。
#3
1
From Apple docs - http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
来自Apple文档 - http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
The UIViewController class provides the fundamental view-management model for all iOS apps.
UIViewController类为所有iOS应用程序提供基本的视图管理模型。
Basically it is the container for your views and handles more logic than just the visual display that your view does.
基本上它是视图的容器,并且处理的逻辑不仅仅是视图所做的视觉显示。
Where necessary, a view controller:
必要时,视图控制器:
- resizes and lays out its views
调整并阐述其观点
- adjusts the contents of the views
调整视图的内容
- acts on behalf of the views when the user interacts with them
当用户与其交互时代表视图行事
#4
1
You should see a "View Controller" as a "Class that controles the view".
您应该将“视图控制器”视为“控制视图的类”。
This is a classic Model View Controller pattern (MVC).
这是一个经典的模型视图控制器模式(MVC)。
A UIViewController
has a view of type UIView
and you update this UIView
depending on your business objects (Model) for each particular controller.
UIViewController具有UIView类型的视图,您可以根据每个特定控制器的业务对象(Model)更新此UIView。
Also, events that occur on your UIView
, are sent back to your UIViewController
for handling them properly.
此外,UIView上发生的事件将被发送回您的UIViewController,以便正确处理它们。