• 10 heures
  • Facile

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 02/09/2019

Discover code

Connectez-vous ou inscrivez-vous gratuitement pour bénéficier de toutes les fonctionnalités de ce cours !

We've completed the design, but programming is writing code, isn't it? It is indeed. And here's where we start doing just that!

Advance!
Advance!

Introducing MVC

The code in iOS applications uses a software architectural pattern called MVC that stands for Model View Controller.

This pattern consists of there elements:

  1. Model: the data and logic of the application.

  2. View: the visuals and interactions - images, labels, buttons, animations, gestures etc.

  3. Controller: the linker between the Model and the View, it's responsible for managing both elements.

Here's a simplified schematic representation:

MVC Architecture
MVC Architecture

The Controller is at the core of this system. It receives the information from the Model (the logic of the app) and displays it in the View.

How's it related to our project?

Let's look at our files:

ViewController.swift file in Navigator
ViewController.swift file in Navigator
  • Main.storyboard: We've already had the opportunity to work with this file to create our interface. The interface is what user sees and interacts with. This is our View element of MVC. 

  • ViewController.swift: As the file name explicitly states: it's a view controller. This is our Controller element of MVC.

And where's the Model?

Good question! Xcode does not generate a separate file for the Model. It's impossible for the tool to predict the structure of our data and the logic of our application. The developers are responsible for accommodating the Model. As you progress in your learning and start creating more and more complex applications, you will be creating multiple files to represent your model. We could create a file  Model.swift  for example, but for the purpose of this fun but simple application, we will use the view controller file that already exists.

Let's take a closer look at the controller.

Inspecting the Controller

As we can see, the file already has some code generated for us by Xcode:

//
// ViewController.swift
// Acty
//
// Created by Olga Volkova on 2017-08-17.
// Copyright © 2017 Olga Volkova OC. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Let's review what we've got:

  • Lines #1-7: This block contains the file name, application name and copyright information that was generated at the time of project creation. 

What are those forward slashes?

As you probably guessed, these are not commands and they won't be translated into machine code to be executed as a part of our application. Each line in this block starts with double forward slash  //  and is called a Comment

Comments are ignored when our application is running, and you can write absolutely anything in any language as comments in your code.

I encourage you to use them often to provide explicit details about your code for future reference ;)

Comment! Comment! Comment!
White your thoughts down!
  • Line #9:  import UIKit: We briefly talked about the UIKit earlier, when we learned about frameworks. UIKit is a core iOS framework that provides the essentials to work with UI. Our controller will need it to manage the interface we've designed, so we import this framework.

  • Line #11:  Class ViewController: UIViewController {  This line defines our controller, it starts with an opening brace and ends with a closing brace on line #24. 

Within the ViewController class definition we've got some code that Xcode generated by default. We will not need it for our app and can simply remove this content. Your file will then look like this:

//
// ViewController.swift
// Acty
//
// Created by Olga Volkova on 2017-08-17.
// Copyright © 2017 Olga Volkova OC. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// We will write all our code here
}

Let's Recap!

  • The code in iOS applications is organized following MVC pattern: Model View Controller. The Controller receives the information from the Model responsible for the logic and displays it in the View.

  • Our application code will be written within this pre-generated block  class ViewController: UIViewController {/* We will write our code here */}

In the next chapter, we will connect our interface to the code!

Exemple de certificat de réussite
Exemple de certificat de réussite