I have been working with MVC for a long time, but I'm not sure that I use this template correctly in iOS.
This is my understanding and the source code that I use for divisions in the model view and controller.
Description:
Model (for example, MyModel class) Model is my data. I use the model for a specific calculation, collecting data from the Internet and then notify the dispatcher about changes in the model, for example, through NSNotificationCenter.
Controller (for example, the MyController class) The controller can directly contact the request for its model data and go directly to the displayed view.
Viewing (for example, the MyView class) Viewing - displaying and collecting events from users. The view can interact with the controller through target action and delegation.
the 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; }
MyController class: 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 a target to my button - my_target (my_target is my MyController). When the user clicks my button method, it is executed in MyController and requests data from my MyData class.
I would like to know where my mistake is in using this method in MVC.
source share