DPS923 notes – Thu Jan 22

General MVC discussion. Introduction to “model” components. Start work on Lab 2.

.

General MVC discussion and checkpoint

As introduced in the CCC “Model-View-Controller” document:

The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. …

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. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer—for example, model layer.

MVC is central to a good design for a Cocoa application. The benefits of adopting this pattern are numerous. Many objects in these applications tend to be more reusable, and their interfaces tend to be better defined. Applications having an MVC design are also more easily extensible than other applications.

Moreover, many Cocoa technologies and architectures are based on MVC and require that your custom objects play one of the MVC roles.

.

Controllers

In the simple apps that we have worked with, there has been one controller class, named “ViewController”. It is in a source code file named “ViewController.swift”. There’s nothing magical or prescriptive about these names – the ‘new project’ template simply created them that way.

It should be apparent that an app can have many controllers.

When you begin creating your own controllers, we strongly recommend that you use meaningful names. For example, the controller for a list of teacher names should be called something like “TeacherList”.

Views

In the simple apps, there has been one view, also known as a “scene” on the “storyboard”.

It should also be apparent that the storyboard can hold many scenes. The storyboard itself is a source code file, and XML is the language. It includes a collection of <scene> elements (although our simple app has only one <scene> element).

Each scene has an internal ‘object identifier’ (which is a string). How can you show this data? Select the “View Controller” icon on the scene’s dock. Show the Identity Inspector. In the “Document” area, you will see a value for “Object ID”. Well, that’s not a very friendly name. Use the “Label” field to enter a better and friendlier identifier.

.

Model class(es) for our app

In this section, we will design and implement a “model” class for our app. The model class has the responsibility of storing and maintaining the app’s data. The other classes in the app – view controllers for example – can access the model class properties and call its methods to access and perform operations on the app’s data.

A model class provides a nice centralized way to store and maintain the app’s data.

It is possible to have more than one model class for your app.

.

Creating a model class

Select the top-level group (folder icon) in your project. On the Xcode “File” menu, choose New > File. Or, simply press Command+N.

Choose/select a Swift File.

Add a class definition statement.

Add properties and methods as required.

FileNewSwiftFileModelClass

.

In a controller, you can add a property that initializes a model object. Next week, you will learn a better way to do this.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: