Model-View-Controller

Before you begin your application, let’s discuss a key concept in application architecture: Model-View-Controller, or MVC. MVC is a design pattern used in iOS development. In MVC, every instance belongs to either the model layer, the view layer, or the controller layer. (Layer here simply refers to one or more objects that together fulfill a role.)

  • The model layer holds data and knows nothing about the user interface, or UI. In Quiz, the model will consist of two ordered lists of strings: one for questions and another for answers.

    Usually, instances in the model layer represent real things in the world of the user. For example, when you write an app for an insurance company, your model will almost certainly contain a custom type called InsurancePolicy.

  • The view layer contains objects that are visible to the user. Examples of view objects, or views, are buttons, text fields, and sliders. View objects make up an application’s UI. In Quiz, the labels showing the question and answer and the buttons beneath them are view objects.

  • The controller layer is where the application is managed. Controller objects, or controllers, are the managers of an application. Controllers configure the views that the user sees and make sure that the view and model objects stay synchronized.

    In general, controllers typically handle And then? questions. For example, when the user selects an item from a list, the controller determines what the user sees next.

Figure 1.6 shows the flow of control in an application in response to user input, such as the user tapping a button.

Figure 1.6  MVC pattern

MVC pattern

Notice that models and views do not talk to each other directly; controllers sit squarely in the middle of everything, receiving messages and dispatching instructions.

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

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