IOS-MVC design mode
The MVC design pattern is essentially when the class functions are categorized and designed, the old man at Stanford University said in the iOS course that view does not care about the data entity, and the view notifies the controller by proxy that it is being manipulated, allowing the controller to determine the operation of the program. such as logical jump or page jump; model does not care Ui,model responsible for the management of data entities, such as the acquisition of data entities from the network, generally designed as a singleton mode, the addition of data entities to delete changes, and these operations model through the Notification Center notify the controller, The controller invokes the corresponding action method to update the UI on the main thread by listening for the corresponding notification. View and model cannot communicate directly. They all communicate through controllers, which are the equivalent of a bridge between view and model.
The simple MVC design is as follows: (Code has comments)
Design of Model:
Model header File:
#import <foundation/foundation.h>extern nsstring *const entityaddnotification;extern NSString *const entitydeletenotification; @interface modelinstance:nsobject+ (Modelinstance *) sharedobject;-(void) Adddataentity: ( ID) entity;-(void) removedataentity: (ID) entity; @end
Model implementation file:
#import "ModelInstance.h"//MVC notification constants when the model layer changes, notify the controller layer in MVC through the Notification center extern nsstring *const [email protected] " AddNotification "; extern nsstring *const [email protected]" deletenotification "; static modelinstance *_shareobject;@ Implementation modelinstance{Nsmutablearray *dataqueue;} + (Modelinstance *) sharedobject{if (_shareobject = = nil)//guaranteed static _shareobject pointer is initialized only once _shareobject = [[Self alloc] INIT]; return _shareobject;} -(Instancetype) init{if (self = [super init]) {} return to self;} Adding a model entity sends an Add entity notification that tells MVC controller-(void) adddataentity: (ID) entity{//Send added notification NSLog (@ "------%s", __func__); [[Nsnotificationcenter Defaultcenter] postnotificationname:entityaddnotification object:entity];} Deleting the model entity sends a DELETE entity notification that tells MVC controller-(void) removedataentity: (ID) entity{//Send delete notification NSLog (@ "------%s", __func__ ); [[Nsnotificationcenter Defaultcenter] postnotificationname:entitydeletenotification object:entity];} @end
Design of the View layer:
Header file for view layer:
#import <UIKit/UIKit.h> @class The Viewinstance;//mvc of the view layer in design mode, when the user operates the view, It invokes the proxy method in the controller through DELEAGTE, so that the controller can do the appropriate processing @protocol viewinstancedelegate <nsobject>// Controller to implement the modification Method-(void) Touchviewinstance: (viewinstance *) view; @end The view proxy in @interface Viewinstance:uiview//mvc, The delegate pointer generally points to the controller instance of the method that implements the proxy @property (nonatomic,assign) id<viewinstancedelegate> delegate; @end
Implementation files for the view layer:
#import "ViewInstance.h" @implementation viewinstance//when View is touched-(void) touchesended: (Nsset *) touches withevent: ( Uievent *) event{ NSLog (@ "------%s", __func__); if ([Self.delegate respondstoselector: @selector (touchviewinstance:)]) { //When the view layer is manipulated, invoke the proxy method, That is to call the controller implementation of the view protocol method [Self.delegate touchviewinstance:self];} } @end
Controller Layer Design:
Controller header file:
#import <UIKit/UIKit.h> @interface Controllerinstance:uiviewcontroller@end
Controller implementation file:
#import "ControllerInstance.h" #import "ModelInstance.h" #import "ViewInstance.h" @interface controllerinstance () < Viewinstancedelegate> @end @implementation controllerinstance-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view. Listen for notification of changes in data sources in MVC [self adddatamodelnotification]; New View Viewinstance *vinstance = [[Viewinstance alloc]initwithframe:cgrectmake (40, 40, 100, 100)]; Set the proxy for view vinstance.delegate = self; Vinstance.backgroundcolor = [Uicolor Redcolor]; [Self.view addsubview:vinstance]; Simulate add and delete operations from MVC Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (nsec_per_sec)), Dispatch_get_main_ Queue (), ^{//20 seconds after analog add operation [[Modelinstance Sharedobject] adddataentity:nil]; 20 seconds after the simulated delete operation Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (nsec_per_sec)), Dispatch_get_main_queu E (), ^{[[Modelinstance Sharedobject] RemOvedataentity:nil]; }); }); }//Monitor Model Change notification-(void) adddatamodelnotification{[[Nsnotificationcenter Defaultcenter] addobserver:self selector:@ Selector (addentitynotification) name:@ "AddNotification" object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (deleteentitynotification) name:@ " Deletenotification "Object:nil";} The action for adding entity notifications is typically used to update ui-(void) addentitynotification{NSLog (@ "------%s", __func__) on the main thread; NSLog (@ "Data source has delete action, you should synchronize UI in main thread");} The action to delete entity notifications is typically used to update ui-(void) deleteentitynotification{NSLog (@ "------%s", __func__) on the main thread; NSLog (@ "Data source has delete action, you should synchronize UI in main thread");} Called after the view is touched, generally used to synchronize data-(void) Touchviewinstance: (viewinstance *) view{NSLog (@ "------%s", __func__); NSLog (@ "View instance is touched you can do the right thing, such as changing the data source");} @end
Running result: Output after 20 seconds of operation, and after 20 seconds or so, the output of touch view is as follows:
To simulate the added output:
2015-04-02 22:49:57.788 target_action[1614:65387]-------[modelinstance adddataentity:]
2015-04-02 22:49:57.789 target_action[1614:65387]-------[controllerinstance addentitynotification]
2015-04-02 22:49:57.789 target_action[1614:65387] data Source has delete action, you should synchronize UI in main thread
Simulation of the deleted output:
2015-04-02 22:50:19.789 target_action[1614:65387]-------[modelinstance removedataentity:]
2015-04-02 22:50:19.789 target_action[1614:65387]-------[controllerinstance deleteentitynotification]
2015-04-02 22:50:19.789 target_action[1614:65387] Data Source has delete action, you should synchronize in main thread UI
Results of touch View output
2015-04-02 22:50:50.598 target_action[1614:65387]-------[viewinstance touchesended:withevent:]
2015-04-02 22:50:50.599 target_action[1614:65387]-------[controllerinstance touchviewinstance:]
2015-04-02 22:50:50.599 target_action[1614:65387] View instance be touched you can do the right thing, like changing the data source
Summary:
IOS-MVC design mode