https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html#//apple_ref/doc/uid/TP40010810-CH14-SW1
- Introduction
- Class Clusters
- Class Factory Methods
- Delegates and Data Sources
- Introspection
- Object Allocation
- Object Initialization
- Model-View-Controller
- Object Modeling
- Object Mutability
- Outlets
- Receptionist Pattern
- Target-Action
- Toll-Free Bridging
- Revision History
Model-View-Controller
The
Object-oriented programs benefit in several ways by adapting the MVC design pattern for their designs. Many objects in these programs tend to be more reusable and their interfaces tend to be better defined. The programs overall are more adaptable to changing requirements—in other words, they are more easily extensible than programs that are not based on MVC. Moreover, many technologies and architectures in Cocoa—such as bindings, the document architecture, and scriptability—are based on MVC and require that your custom objects play one of the roles defined by MVC.
Roles and Relationships of MVC Objects
The MVC design pattern considers there to be three types of objects: model objects, view objects, and controller objects. The MVC pattern defines the roles that these types of objects play in the application and their lines of communication. When designing an application, a major step is choosing—or creating custom classes for—objects that fall into one of these three groups. Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries.
Model Objects Encapsulate Data and Basic Behaviors
Model objects represent special knowledge and expertise. They hold an application’s data and define the logic that manipulates that data. A well-designed MVC application has all its important data encapsulated in model objects. Any data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects once the data is loaded into the application. Because they represent knowledge and expertise related to a specific problem domain, they tend to be reusable.
Ideally, a model object has no explicit connection to the user interface used to present and edit it. For example, if you have a model object that represents a person (say you are writing an address book), you might want to store a birthdate. That’s a good thing to store in your Person model object. However, storing a date format string or other information on how that date is to be presented is probably better off somewhere else.
In practice, this separation is not always the best thing, and there is some room for flexibility here, but in general a model object should not be concerned with interface and presentation issues. One example where a bit of an exception is reasonable is a drawing application that has model objects that represent the graphics displayed. It makes sense for the graphic objects to know how to draw themselves because the main reason for their existence is to define a visual thing. But even in this case, the graphic objects should not rely on living in a particular view or any view at all, and they should not be in charge of knowing when to draw themselves. They should be asked to draw themselves by the view object that wants to present them.
View Objects Present Information to the User
A
View objects tend to be reusable and configurable, and they provide consistency between applications. In Cocoa, the AppKit framework defines a large number of view objects and provides many of them in the Interface Builder library. By reusing the AppKit’s view objects, such as NSButton
A view should ensure it is displaying the model correctly. Consequently, it usually needs to know about changes to the model. Because model objects should not be tied to specific view objects, they need a generic way of indicating that they have changed.
Controller Objects Tie the Model to the View
A
In a typical Cocoa MVC design, when users enter a value or indicate a choice through a view object, that value or choice is communicated to a controller object. The controller object might interpret the user input in some application-specific way and then either may tell a model object what to do with this input—for example, "add a new value" or "delete the current record"—or it may have the model object reflect a changed value in one of its properties. Based on this same user input, some controller objects might also tell a view object to change an aspect of its appearance or behavior, such as telling a button to disable itself. Conversely, when a model object changes—say, a new data source is accessed—the model object usually communicates that change to a controller object, which then requests one or more view objects to update themselves accordingly.
Controller objects can be either reusable or nonreusable, depending on their general type.
Combining Roles
One can merge the MVC roles played by an object, making an object, for example, fulfill both the controller and view roles—in which case, it would be called a
A NSDocument
A NSWindowController
object (also part of the document architecture) is an example of a view controller.
Design Guidelines for MVC Applications
Further Reading:
Types of Cocoa Controller Objects
Controller Objects Tie the Model to the View
A NSController
class. Mediating controller objects are used in the Cocoa
iOS Note: NSController
Mediating controllers are typically ready-made objects that you drag from the Interface Builder library. You can configure these objects to establish the bindings between properties of view objects and properties of the controller object, and then between those controller properties and specific properties of a model object. As a result, when users change a value displayed in a view object, the new value is automatically communicated to a model object for storage—via the mediating controller; and when a property of a model changes its value, that change is communicated to a view for display. The abstract NSController
NSObjectController
, NSArrayController
, NSUserDefaultsController
, and NSTreeController
—provide supporting features such as the ability to commit and discard changes and the management of selections and placeholder values.
A NSWindowController
NSDocumentController
object (available only in AppKit), or an instance of a custom subclass ofNSObject
. Its role in an application is to oversee—or coordinate—the functioning of the entire application or of part of the application, such as the objects unarchived from a nib file. A coordinating controller provides services such as:
-
Responding to delegation messages and observing notifications
-
Responding to action messages
-
Managing the life cycle of owned objects (for example, releasing them at the proper time)
-
Establishing connections between objects and performing other set-up tasks
NSWindowController
NSDocumentController
NSWindowController
A coordinating controller frequently owns the objects archived in a nib file. As File’s Owner, the coordinating controller is external to the objects in the nib file and manages those objects. These owned objects include mediating controllers as well as window objects and view objects. See
Instances of custom NSObject
MVC as a Compound Design Pattern
Model-View-Controller is a design pattern that is composed of several more basic design patterns. These basic patterns work together to define the functional separation and paths of communication that are characteristic of an MVC application. However, the traditional notion of MVC assigns a set of basic patterns different from those that Cocoa assigns. The difference primarily lies in the roles given to the controller and view objects of an application.
In the original (Smalltalk) conception, MVC is made up of the Composite, Strategy, and Observer patterns.
-
Composite—The view objects in an application are actually a composite of nested views that work together in a coordinated fashion (that is, the view hierarchy). These display components range from a window to compound views, such as a table view, to individual views, such as buttons. User input and display can take place at any level of the composite structure.
-
Strategy—A controller object implements the strategy for one or more view objects. The view object confines itself to maintaining its visual aspects, and it delegates to the controller all decisions about the application-specific meaning of the interface behavior.
-
Observer—A model object keeps interested objects in an application—usually view objects—advised of changes in its state.
The traditional way the Composite, Strategy, and Observer patterns work together is depicted by

The Cocoa version of MVC as a compound pattern has some similarities to the traditional version, and in fact it is quite possible to construct a working application based on the diagram in
In most Cocoa applications, notifications of state changes in model objects are communicated to view objects

The controller object in this compound design pattern incorporates the
Note:
There are practical reasons as well as theoretical ones for the revised compound design pattern depicted in NSController
, and these classes, besides implementing the Mediator pattern, offer many features that applications should take advantage of, such as the management of selections and placeholder values. And if you opt not to use the bindings technology, your view object could use a mechanism such as the Cocoa notification center to receive notifications from a model object. But this would require you to create a custom view subclass to add the knowledge of the notifications posted by the model object.
In a well-designed Cocoa MVC application, coordinating controller objects often own mediating controllers, which are archived in nib files.

Design Guidelines for MVC Applications
The following
-
Although you can use an instance of a custom subclass of
NSObject
as a mediating controller, there's no reason to go through all the work required to make it one. Use instead one of the ready-made NSController
objects designed for the Cocoa bindings technology; that is, use an instance of NSObjectController
,NSArrayController
,NSUserDefaultsController
, orNSTreeController
—or a custom subclass of one of these concreteNSController
subclasses. However, if the application is very simple and you feel more comfortable writing the glue code needed to implement mediating behavior using outlets and target-action, feel free to use an instance of a custom
NSObject
subclass as a mediating controller. In a custom NSObject
subclass, you can also implement a mediating controller in the NSController
sense, using key-value coding, key-value observing, and the editor protocols. -
Although you can combine MVC roles in an object, the best overall strategy is to keep the separation between roles. This separation enhances the reusability of objects and the extensibility of the program they're used in. If you are going to merge MVC roles in a class, pick a predominant role for that class and then (for maintenance purposes) use categories in the same implementation file to extend the class to play other roles.
-
A goal of a well-designed MVC application should be to use as many objects as possible that are (theoretically, at least) reusable. In particular, view objects and model objects should be highly reusable. (The ready-made mediating controller objects, of course, are reusable.) Application-specific behavior is frequently concentrated as much as possible in controller objects.
-
Although it is possible to have views directly observe models to detect changes in state, it is best not to do so. A view object should always go through a mediating controller object to learn about changes in an model object. The reason is two-fold:
-
If you use the bindings mechanism to have view objects directly observe the properties of model objects, you bypass all the advantages that
NSController
and its subclasses give your application: selection and placeholder management as well as the ability to commit and discard changes. -
If you don't use the bindings mechanism, you have to subclass an existing view class to add the ability to observe change notifications posted by a model object.
-
-
Strive to limit code dependency in the classes of your application. The greater the dependency a class has on another class, the less reusable it is. Specific recommendations vary by the MVC roles of the two classes involved:
-
A view class shouldn't depend on a model class (although this may be unavoidable with some custom views).
-
A view class shouldn't have to depend on a mediating controller class.
-
A model class shouldn't depend on anything other than other model classes.
-
A mediating controller class shouldn’t depend on a model class (although, like views, this may be necessary if it's a custom controller class).
-
A mediating controller class shouldn't depend on view classes or on coordinating controller classes.
-
A coordinating controller class depends on classes of all MVC role types.
-
-
If Cocoa offers an architecture that solves a programming problem, and this architecture assigns MVC roles to objects of specific types, use that architecture. It will be much easier to put your project together if you do. The document architecture, for example, includes an Xcode project template that configures an
NSDocument
object (per-nib model controller) as File's Owner.
Model-View-Controller in Cocoa (OS X)
The
Cocoa in OS X includes the following architectures, mechanisms, and technologies that are based on Model-View-Controller:
-
Document architecture. In this architecture, a document-based application consists of a controller object for the entire application (
NSDocumentController
), a controller object for each document window (NSWindowController
), and an object that combines controller and model roles for each document (NSDocument
). -
Bindings. MVC is central to the bindings technology of Cocoa. The concrete subclasses of the abstract
NSController
provide ready-made controller objects that you can configure to establish bindings between view objects and properly designed model objects. -
Application scriptability. When designing an application to make it scriptable, it is essential not only that it follow the MVC design pattern but that your application’s model objects are properly designed. Scripting commands that access application state and request application behavior should usually be sent to model objects or controller objects.
-
Core Data. The Core Data framework manages graphs of model objects and ensures the persistence of those objects by saving them to (and retrieving them from) a persistent store. Core Data is tightly integrated with the Cocoa bindings technology. The MVC and object modeling design patterns are essential determinants of the Core Data architecture.
-
Undo. In the undo architecture, model objects once again play a central role. The primitive methods of model objects (which are usually its accessor methods) are often where you implement undo and redo operations. The view and controller objects of an action may also be involved in these operations; for example, you might have such objects give specific titles to the undo and redo menu items, or you might have them undo selections in a text view.
Copyright © 2012 Apple Inc. All Rights Reserved.