title: Design Pattern - Objective-C - MVC Model View Controller
body: <p>Hi I already read tutorials around the web on MVC and already read the topics on here. I think i got the concept of the MVC but i'm not sure of its implementation.</p>
  
  <p>I've tried to apply it to a simple program, a window that have a label and a button.
  The button increase a counter and the label shows the value of it.</p>
  
  <p>I tried in 2 different ways. </p>
  
  <p>In the first case ( the example works ) i melt the View and the Controller. As i said, the example works, but i want you guys to tell me if it's a correct implementation for MVC or it's not following the right design. </p>
  
  <p>The second example has Model View and Controller as 3 separated class, but the example doesnt work because the V and the C import itself, so i would love you guys to tell me where i'm doing wrong.</p>
  
  <p><b>first version: model, view-controller</b></p>
  
  <pre><code>//Model.h
  #import &lt;Foundation/Foundation.h&gt;
  
  @interface Model : NSObject {
      int _counter;
  }
  
  -(void)setCounter:(int)valueCounter;
  -(int)getCounter;
  -(void)increaseCounter;
  @end
  
  //Model.m
  #import "Model.h"
  @implementation Model {}
  
  -(void)setCounter:(int)valueCounter { _counter = valueCounter; }
  -(int)getCounter { return _counter; }
  -(void)increaseCounter{ _counter ++; }
  @end
  
  
  //ViewController.h
  #import &lt;UIKit/UIKit.h&gt;
  #import "Model.h"
  
  @interface ViewController : UIViewController {
      IBOutlet UIButton *_button;
      IBOutlet UILabel *_label;
      Model *myModel;
  }
  
  -(IBAction)send:(id)sender;
  @end
  
  //ViewController.m
  #import "ViewController.h"
  @interface ViewController ()
  @end
  
  @implementation ViewController
  - (void)viewDidLoad {
      [super viewDidLoad];
       myModel = [[Model alloc]init];
      _label.text = [NSString stringWithFormat:@"%d",[myModel getCounter]];
  }
  
  - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
  
  - (IBAction)send:(id)sender{
      [myModel increaseCounter];
      _label.text = [NSString stringWithFormat:@"%d",[myModel getCounter]];
  }
  
  @end
  </code></pre>
  
  <p><BR><BR></p>
  
  <p>Is this way a correct Pattern for MVC ? The code works, but before i start more complex apps i want to make sure i code it in a good way. This is how i would do this app, my way of MVC. is it bad? good? how to change or fix it? </p>
  
  <p><BR><BR><BR></p>
  
  <p><b>Second Version: Model, View, Controller separated</b></p>
  
  <p>----> This is the Model</p>
  
  <pre><code>//Model.h
  #import &lt;Foundation/Foundation.h&gt;
  
  @interface Model : NSObject {
      int _count;
  }
  
  -(void)setCount:(int)value;
  -(int)getCount;
  -(void)increaseCount;
  
  @end
  
  //Model.m
  #import "Model.h"
  
  @implementation Model
  
  -(void)setCount:(int)value { _count = value; }
  -(int)getCount { return _count; }
  -(void)increaseCount { _count = _count++; }
  
  @end
  </code></pre>
  
  <p>----> This is the View</p>
  
  <pre><code>//View.h
  #import &lt;UIKit/UIKit.h&gt;
  #import "Controller.h"
  
  @interface ViewController : UIViewController{
      IBOutlet UILabel *label;
      IBOutlet UIButton *button;
      Controller *myController;
  }
  
  @end
  
  //View.m
  #import "ViewController.h"
  #import "Controller.h"
  
  @interface ViewController ()
  @end
  
  @implementation ViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      myController = [[Controller alloc]init];
  }
  
  - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
  
  
  -(IBAction)pressButton:(id)sender{
      label.text = [NSString stringWithFormat:@"%d",[myController actionIncrease]];
  }
  
  @end
  </code></pre>
  
  <p>----> This is the Controller</p>
  
  <pre><code>//Controller.m
  #import &lt;Foundation/Foundation.h&gt;
  
  @class "Model.h"
  @class  "ViewController.h"
  
  @interface Controller : NSObject {
      Model *_mymodel;
      UIViewController *_myviewController;
  }
  
  -(int)actionIncrease;
  
  @end
  
  //Controller.m
  #import "Controller.h"
  #import "Model.h"
  
  @implementation Controller
  
  -(id)init{
      _mymodel = [[Model alloc]init];
  }
  
  -(int)actionIncrease {
      [_mymodel increaseCount];
      return [_mymodel getCount];    
  }
  
  @end
  </code></pre>
  
  <p><br><br>
  this version doesn't work because the classes view and controller import each other and the compiler gives me a warning</p>