Sunday, January 15, 2012

What is MVC? How it is implemented in iPhone development?

MVC, a much known term in software industry right now.  I will not spend much time to explain MVC; rather, how it is implemented in IPhone will get more priority in this article. Let’s start.
MVC - simply Model View Controller.
Now take a simple example. Suppose a car. It has a body, four wheels, one steering and one engine. Now, engine is a module, steering is a controller, wheel is a separate module and guess what, body is view. Pretty simple, right. Now come out from the car. In software, an object which represents project entities is module. Controller will do various operations with modules depending on different commands and commands will be sending user interface [view] and the output result will be shown in view itself.
It’s done.
mvc iphone
MVC Architecture 

Cocoatouch is a building block of iPhone development. It is solely following MVC for development. As we understood previously, it has model, view and controller, iPhone development follows the same.  When we create an application using xCode, it will give us a specific folder structure with the support of MVC itself. In the folder one can see view and viewController files.  There will be two kinds of file or rather we can say same named files with two different extensions [*.h , *.m].

Now, let’s see ,what is in it.
[.H] file will contain something like ----

@interface MainViewController : UIViewController {
UILabel *label;// label is added to it

@property (nonatomic, retain) IBOutlet UILabel *label;
 //will be used as a reference to controller

Here we can see, it is an interface which inherits UIViewController[Master class for all view]. It also contains a label[Added by us].

[.m]file will contain ---
#import "MainViewController.h"
#import "MainView.h"

@implementation MainViewController
@synthesize label; //to do sync properties in interface

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
return self;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data

- (void)dealloc {
[label release];
[super dealloc];


Here, we implement the created interface in .H file as far as MVC concerns.  And it will work with all the properties declared in interface. Here we use @synthesize to synchronize attributes in [.m] and [.h].
Now in this way we created a controller and an interface for view. Sounds confusing?  Yes , View.h file will work as an interface for view [normal MVC concept].  
We will have .xib files too. It is basically an xml which represents the actual UI.

Now , from the toolbox we can get all available controls ,which we can drag and drop to the UI to design as per our requirement. [The process of designing will not be here in this article. It’s all about MVC in iPhone]
Now it is the time to connect the UI to View to interact with controller.

In view controller connection window, we can find all the added controls in a list. By dragging the listed control reference to the View object cube we can map the UI objects to view properties. All the code related to it will be auto generated. So in this way view and controller will be connected.
Now, we can create a custom model to interact with values sent from view. To do that we need to create an interface of a model in myCar.h file-
#import <foundation/Foundation.h>

@interface myCar : NSObject {
    NSString *name;

@property (nonatomic, retain) NSString *name;

+ (myCar *)sharedModel;


Now we are ready to implement the interface in myCar.m file.
#import "myCar.h" 
@implementation myCar \\    here the implementation
@synthesize name;\\ synthesize with interface implementation

- (id) init
    self = [super init];
    if (self != nil) {
        name = @"";
    return self;
- (void) dealloc
    [name release]; \\release memory from device
    [super dealloc];
Our model is also created.  It’s the time to implement model in our code. See bellow-
- (IBAction)textChanged:(id)sender
    myCar *car = [myCar sharedModel];
    model.text = textField.text;

It is just an over view. Explore much by creating an app.
Post a Comment