Objective-C - Design pattern - Model-View-Controller - MVC

The MVC pattern is certainly the most famous one.
It allows to dispatch the work in 3 concepts: The Model, the View and the Controller.
This concept exists in all programming languages.
So it's easy, when you understood it, to apply your knowledge with all others languages.
That's why this design pattern is really important for a good developer, and I am sure you are one of them.

Let's beginning by the Controller.

1. The ViewController

 

a) The header of the ViewController

 

In Objective-C, the ViewController's header (the .h) must import the UIKit/UIKit.h.

#import <UIKit/UIKit.h>

The ViewController inherits from the UIViewController.

@interface BadProgViewController : UIViewController

The interface must have all properties used in the method implementation (the .m). This is a protocol.

{
    UILabel *bpLabel;
    UITextField *bpTextField;
}

The properties have to be declared with the keyword @property.

@property UILabel *bpLabel;
@property UITextField *bpTextField;

The file finishes with the keyword @end.

@end

The complete header file.

//
// BadProgViewController.h
//

#import <UIKit/UIKit.h>

@interface BadProgViewController : UIViewController
{
    UILabel *bpLabel;
    UITextField *bpTextField;
}

@property UILabel *bpLabel;
@property UITextField *bpTextField;

@end

 

b) The methods of the ViewController

 

As the header, the methods have their own file, called .m.
In our case BadProgViewController.m.

The method file has to import the corresponding header.

#import "BadProgViewController.h"

The implementation keyword has to be used in conjunction with the name of the file.

@implementation BadProgViewController

The properties can be synthesize.

@synthesize bpLabel;
@synthesize bpTextField;

Methods can now be created.

...

Do not forget to dealloc variables.

- (void) dealloc
{
    [bpLabel release];
    [bpTextField release];
    [super dealloc];
}

Finish the file with the keyword @end.

@end

The complete methods' file.

//
// BapProgViewController.m
//

#import "BadProgViewController.h"

@implementation BadProgViewController

@synthesize bpLabel;
@synthesize bpTextField;

...

- (void) dealloc
{
    [bpLabel release];
    [bpTextField release];
    [super dealloc];
}

@end

 

Comments

Comment: 

Nice Implementation

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.