iOS Tutorials: Implement MVC in our project

swift 4.2 | xcode 10

OVERVIEW

Model-View-Controller(MVC) is a software architecture that is recommended by Apple which explains why most of the Apple framework uses MVC. On top of that, MVC could help organize your codes in your project.

MVC is made up of three layers – model, view and controller.

  • Model is where you store your data.
  • View is where you create the User Interface such as storyboard.
  • Controller is where you write your business logic and act like a mediator for both view and model .

Let’s create a simple MVC for our project. Go ahead and create your project and organize your code according to what is shown below:

MODEL

In our Food.swift file, this is how we define our data. Currently, we will need to a name and a price for our food.

VIEW

In our Main.storyboard file, we will only be needing additional object such as Table View and Table View Cell. And don’t forget to set the style to subtitle and the identifier as cell.

CONTROLLER

Let’s separate our code and dive into some understanding here. Here in this part of the code, we will be customize our data by inputting some informations and also assigning the data source.

Once we have those, we can create an extension. You don’t have to do this, it’s just more organized this way. The first function basically return however many data we have. For instance, if we have 5 datas, it will return 5 datas. And the following function is where we assign those data into the table cell.

This project is a pretty straight forward illustration of MVC using the simplest data. With this, I hope you get the idea of MVC to properly structure your files into an organized manner.

The project is uploaded to Github

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*