What are controller classes? [duplicate]

2019-07-04 06:51发布

问题:

This question already has an answer here:

  • UIView vs UIViewController 3 answers
  • When to use a UIView vs. a UIViewController on the iPhone? 9 answers
  • What is a UIViewController 1 answer

What are controller classes, like uiviewcontroller? What's the difference between uiviewcontroller and uiview? Thanks!

回答1:

Cocoa works using the Model-Controller-View concept in Object-Oriented Programming

In this system the model consists of objects that store and work with any data being used by the application, the view consists of the classes that actually control the how the application is displayed.

The controllers are objects that act as a go-between. The view gains its data through methods provided by the controller, which deals with the model directly.

There is a good explanation of this concept here: Cocoa Application Tutorial: Essence of Cocoa



回答2:

A bit of history here. Apple/NeXT did not used to include any predefined controller classes until about 10.4 or 10.5. Prior, everyone wrote their own controller classes. Apple the started creating NSWindowController, NSViewController, etc. so that programmers did not have to reinvent the wheel.

In fact, you don't have to us Apple's controller classes at all and just write your own. It's actually a good learning experience.