IOS Proxy Way to implement the example detailed _ios

Source: Internet
Author: User
Tags reserved

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.