Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 294
Matrosov Oleksandr Avatar asked Aug 20 '11 10:08

Matrosov Oleksandr


People also ask

Does iOS use MVC?

Model-View-Controller is a fundamental concept to understand, especially in iOS development. Many iOS frameworks, like UIKit, use the MVC pattern to structure data flow and messaging.

How does MVC work in iOS?

MVC is a software development pattern made up of three main objects: The Model is where your data resides. Things like persistence, model objects, parsers, managers, and networking code live there. The View layer is the face of your app.

Does iOS use MVC or MVVM?

MVVM offers some advantages over Model-View-Controller, or MVC, which is the de facto approach in iOS: Reduced complexity: MVVM makes the view controller simpler by moving a lot of business logic out of it.

Is iOS development MVC?

MVC is the most-known architecture pattern when it comes to iOS development. In this first article, we saw it in action being applied to a small application. We used the naive approach, where each screen is represented by a View Controller.


1 Answers

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.

like image 158
Caleb Avatar answered Nov 30 '22 05:11

Caleb