Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

How best to use MVC in iOS

I work for a long time with MVC but isn't assured that correctly I use this pattern in iOS.

This is my understanding and source code which i use for divisions on model view and controller.

Description:

  1. Model (for example - class MyModel) Model this is my data. I use model for defined calculation, data acquisition from the Internet and further I notify the controller on changes in model for example through the NSNotificationCenter.

  2. Controller (for example - class MyController) The controller can directly contact the request of its model data, and go directly to the display in view.

  3. View (for example - class MyView) View - display and gathering of events from users. View can interaction with controller through target-action and delegate.

Code:

class MyModel:NSObject

    .h ... (some header code)
    .m
    Initialization method...

    // method for get data from internet
    -(NSData *)my_getDataFromInternet:(NSURL *)url{
       NSData *data=[NSData dataWithContentsOfURL:url];
       return data;    
    }

class MyController:UIVIewController

     #import "MyView.h"
     .h
     MyView * my_view;

     #import "MyData.h"
     .m
     Initialization method...
     - (void)init{
        my_view = [[MyView alloc]init];
        my_view.my_target = self;
        self.view = my_view;
     }

     -(void)mycontrolleraction{
        MyData * my_data = ...
        [my_data my_getDataFromInternet:some_url_image];
        my_view.my_image = [UIImage imageWithData:self.my_data];
     }

class MyView:UIView

     .h
     UIImage * my_image;
     property(nonatomic, assign)id my_target;
     .m
     Initialization method...
     - (void)initWithFrame{
         UIButton * my_button = ...
         [button addTarget:my_target ....
         my_image = ...
         [self addSubview:my_image];
         [self addSubview:my_button];
      }

I add target to my button - my_target (my_target - this is my MyController). When user tap in my button - method is executed in the MyController and ask data from my MyData class.

I would like to know where my mistake in using this method in the MVC.

Upvotes: 6

Views: 2746

Answers (1)

Caleb
Caleb

Reputation: 124997

It looks like you've got the right idea. I usually think of the model as something that stores the data as well as operating on it, so it seems a little odd to have the model fetch the image and then just return it without storing it. Having the model hold onto the data would let it avoid having to fetch it again later, but the way you have it isn't wrong, and where the data comes from is something that should be entirely up to the model.

One thing I'd suggest, not related to MVC, is to follow the convention for initializers. Your initialization methods must call the superclass's designated initializer, so your controller's -init should look like:

-(id)init
{
    if ((self = [super init])) {    // double parens to avoid warning about = vs ==
        my_view = [[MyView alloc] init];  // assuming my_view is an ivar
        my_view my_target = self;
    }
    return self;
}

The same goes for your view and model classes.

Upvotes: 3

Related Questions