IOS Proxy Mode implementation
In client development, notifications, proxies, and blocks are often used to implement the links between individual pages. notification, which is passed in a "blind" manner. Agent, Block
You can clearly understand the relationship between the various interfaces. In the case of agents, the general practice is as follows:
Desviewcontroller *des = [[Desviewcontroller alloc] init];d es.delegate = self; [Self.navigationcontroller pushviewcontroller:des Animated:yes];
In this case, the general two interface is a certain relationship, such as: from a interface to the B interface or a view is a part of the controller. However, if there is no contact with how to deal with it, for example: a interface needs to be based on user login status to display different data, or show different interface situation:
Actual combat:
Idea: Create a management Class class processing, set up the corresponding proxy method, and then when needed, add or delete the corresponding proxy method.
Core code:
. h Files
rsloginservice.h//iosdelegate////Created by admin on 2016/11/6.//copyright©2016 year Reading. All rights reserved.
#import <Foundation/Foundation.h> @protocol userloginstatusdelegate <NSObject>
-(void) Userdidloginin;
-(void) userwillloginout;
@end @interface rsloginservice:nsobject+ (instancetype) sharedinstance;
@property (nonatomic, strong) Nsmutableset *delegates;
-(void) onwillloginout;
-(void) ondidloginin;
-(void) AddDelegate: (id<userloginstatusdelegate>) delegate;
-(void) Removedelegate: (id<userloginstatusdelegate>) delegate; @end
. m file
rsloginservice.m//iosdelegate////Created by admin on 2016/11/6. COPYRIGHT©2016 year Reading.
All rights reserved. #import "RSLoginService.h" @implementation rsloginservice+ (instancetype) sharedinstance{static ID instance; Static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{instance = [[Rsloginservice alloc] init]; }); return instance;} -(void) onwillloginout{//Do something your need to does before Login out [Self.delegates makeobjectsperformselector:@s Elector (Userwillloginout)];} -(void) ondidloginin{//Do something your need to does after Login in [self.delegates makeobjectsperformselector: @sel Ector (Userdidloginin)];} -(void) AddDelegate: (id<userloginstatusdelegate>) delegate{if (![ Self.delegates Containsobject:delegate]) {[Self.delegates addobject:delegate]; (void) Removedelegate: (id<userloginstatusdelegate>) delegate{if (![ Self.delegates Containsobject:delegate]) {[Self.delegates removeobject:delegate];}}-(Nsmutableset *) delegates{if (!_delegates) {_delegates = [nsmutableset set]; return _delegates;}
@end
Thank you for reading, I hope to help you, thank you for your support for this site!