Model-View-Controller

You may hear iOS programmers mention the Model-View-Controller pattern. What this means is every object you create is exactly one of the following: a model object, a view object, or a controller object.

View objects are visible to the user. In Quiz, the buttons, labels, and window are all view objects. Views are usually standard UIView subclasses (UIButton, UISlider), but you will sometimes write custom view classes. These typically have names like DangerMeterView or IncomeGraphView.

Model objects hold data and know nothing about the user interface. In this application, the model objects will be two arrays of strings: the questions array and the answers array. Figure 1.11 displays the object diagram of the Quiz application’s model objects.

Figure 1.11  Diagram of model objects in Quiz

Diagram of model objects in Quiz

Model objects typically use standard collection classes (NSArray, NSDictionary, NSSet) and standard value types (NSString, NSDate, NSNumber). But there can be custom classes, which typically have names that sound like data-bearing objects, such as InsurancePolicy or PlayerHistory.

View and model objects are the factory workers of an application – they focus only on performing specific tasks. For example, an instance of UILabel (a view object) knows how to display text in a given font within a given rectangle. An NSString instance (a model object) knows how to store a character string. But the label doesn’t know what text to display, and the string doesn’t know what characters to store.

This is where controller objects come in. Controllers are the managers in an application. They keep the view and model objects in sync, control the flow of the application, and save the model objects out to the filesystem (Figure 1.12). Controllers are the least reusable classes that you will write, and they tend to have names like ScheduleController and ScoreViewController.

Figure 1.12  MVC Pattern

MVC Pattern

When you create a new iOS project from a template, the template automatically makes a controller object for you. For Quiz, this controller is the QuizAppDelegate. Most applications have more than one controller object, but a simple application like Quiz only needs one.

One of the QuizAppDelegate’s tasks will be showing the user a new question when the Show Question button is tapped. Tapping this button will trigger a method in the QuizAppDelegate. This method will retrieve a new question from an array of questions and place that question in one of the labels. These interactions are laid out in the object diagram for Quiz (Figure 1.13).

Figure 1.13  Object diagram for Quiz

Object diagram for Quiz

This diagram is the big picture of Quiz. It’s okay if it doesn’t make perfect sense yet; it will make more by the end of the chapter. We’ll talk about object diagrams again in Chapter 5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.151