欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS 代理方式實(shí)現(xiàn)實(shí)例詳解

 更新時(shí)間:2016年11月22日 08:53:07   投稿:lqh  
這篇文章主要介紹了IOS 代理方式實(shí)現(xiàn)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

IOS 代理方式實(shí)現(xiàn)

在客戶端開發(fā)中,經(jīng)常用到通知、代理、block來實(shí)現(xiàn)各個(gè)頁面之間關(guān)聯(lián)。通知,以一直“盲”的方式實(shí)現(xiàn)傳遞。 代理、block
可以很明確的知道各個(gè)界面之間的關(guān)聯(lián)關(guān)系。以代理為例,一般的做法如下 :

DesViewController *des = [[DesViewController alloc] init];des.delegate = self;[self.navigationController pushViewController:des animated:YES];

這種情況下,一般兩個(gè)界面是有一定的關(guān)系的,例如:從A界面跳轉(zhuǎn)到B界面或者a的視圖是A控制器之間一部分。但是,如果沒有聯(lián)系的怎么處理呢,例如: A界面需要根據(jù)用戶登錄狀態(tài)來展示不同的數(shù)據(jù),或者展示不同的界面情況:

實(shí)戰(zhàn):

思路: 創(chuàng)建一個(gè)管理類類處理,設(shè)置好對(duì)應(yīng)的代理方法,然后再需要的時(shí)候,添加 或者 刪除對(duì)應(yīng)的代理方法即可。

核心代碼:

.h文件

//// RSLoginService.h// iOSDelegate//// Created by admin on 2016/11/6.// Copyright © 2016年 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文件

//// RSLoginService.m// iOSDelegate//// Created by admin on 2016/11/6.

// Copyright © 2016年 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 you need to do before Login out 
 [self.delegates makeObjectsPerformSelector:@selector(userWillLoginOut)];}- (void)onDidLoginIn{  

 // do something you need to do after Login In  
 [self.delegates makeObjectsPerformSelector:@selector(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

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論