How to Implement MVC in iOS Using Xcode

If you have just started to learn iOS Application programming, Some of the questions that come to your mind could be: “What is the best way to program in Xcode IDE?”, “Should I use purely coding? Code + Interface Builder? or Code + Storyboard?”

From my own experience working with some iOS apps developers, I learned that some of them are still using pure coding with some Interface Builder. It means that they haven’t adopt the latest technologies in Xcode provided by Apple. I can not totally blame them, it is because they are developing iOS projects that have been there for a few years and to change everything to the latest technologies is not an easy task to do.

For a fresh new iOS project, it is the best to adopt the latest technologies provided by Apple. It means that we have to use both Code + Storyboard to make the iOS applications.

MVC in Objective-C and Xcode

How do we implement MVC Using Xcode? Here is the quick summary:-

Model: Model is always in coding. It can be in its own class (with .h and .m). But sometimes, we can code a simple Model inside a view controller.
Controller: It always has its own class (with .h and .m). However, Apple has done a great job by making us to be able to see the view Controller in Storyboard. So, we can create a dummy (without function) view controller object on Storyboard. Then, we link this view controller UI Object with our own custom class file (.h and .m). We can code the functions inside the .h and .m files.
View: You can display a view using coding but it is not recommended since the release of storyboard. So, we always create view objects on Storyboard. We just drag and drop the GUI Objects into the View Controller. If we need to have special customization of object, we can subclass that object and manipulate it using code. But most of the time, we do not have to do that as the existing customization properties on the Xcode is already good enough. When a certain objects want to communication with View controller using (Target-Action), we just need to CONTROL drag that element into the .m file. As for Delegate and DataSource, we will implement it on the View Controller files.

Here is the detail explanation on Model, View and Controller with some visual explanations.

If you take a quick look at the Xcode, you will find out that there are many ready to use GUI (Graphic User Interface) Objects such as Button, Text Field, View and even View Controller. You can easily drag and drop the GUI elements into the storyboard. So, Views are mostly available on the storyboard.
View Controller Objects

There are a lot of customization properties inside the view object as well. For Example, you can change the shape of a button, change the title, color, font, image, background image, shadow and more.
UIButton Properties

If you remember the previous post on: Programming in iOS with Objective-C Using MVC, A view can only communicate with view controller using 3 different methods and Target-Action is one of them.

The below are the steps for creating Target-Action for Button to inform the View Controller when the user has clicked the button.

Step 1: Control + Drag the button into the .m implementation file.
Target-Action 1
Step 2: Enter a function Name and also type of sender.
Target-Action 2
Step 3: A function is created and you can start to code the body of the function.
Target-Action 3

For View controller, the below shows the .h and .m files.
dot H and dot M files for view controller

We can always link the custom class (.h and .m) with the view controller object on the storyboard. We can implement various functions using coding.
Custom class in Xcode

Simple Calculator iOS Application
Programming in iOS with Objective-C Using MVC

Speak Your Mind