ios代理设计模式

   代理设计模式的作用:
     1.A对象监听B对象的一些行为,A成为B的代理
     2.B对象想告诉A对象一些事情,A成为B的代理
   代理设计模式的总结:
     如果你想监听别人的一些行为,那么你就要成为别人的代理
     如果你想告诉别人一些事情,那么就让别人成为你的代理
     
    代理设计模式的开发步骤
     1.拟一份协议(协议名字的格式:控件名 + Delegate),在协议里面声明一些代理方法(一般代理方法都是@optional)
     2.声明一个代理属性:@property (nonatomic, weak) id<代理协议> delegate;
     3.在内部发生某些行为时,调用代理对应的代理方法,通知代理内部发生什么事
     4.设置代理:xxx.delegate = yyy;
     5.yyy对象遵守协议,实现代理方法

 1 #import <UIKit/UIKit.h>
 2 
 3 @class XMGLoadMoreFooter;
 4 
 5 @protocol XMGLoadMoreFooterDelegate <NSObject>
 6 @optional//不写@optional默认为必须实现的代理方法
 7 //代理方法一般以类名开头,传入被监听的控件对象
 8 - (void)loadMoreFooterDidClickLoadMoreButton:(XMGLoadMoreFooter *)footer;
 9 @end
10 
11 @interface XMGLoadMoreFooter : UIView
12 + (instancetype)footer;
13 //代理属性用weak修饰,id类型代表任何类都可以作为代理,在<>中指明要实现的协议名称
14 @property (nonatomic, weak) id<XMGLoadMoreFooterDelegate> delegate;
15 
16 /**
17  * 结束加载状态
18  */
19 - (void)endLoading;
20 @end
h文件

 

 1 //
 2 //  XMGLoadMoreFooter.m
 3 //  06-自定义等高cell01-storyboard
 4 //
 5 //  Created by xiaomage on 15/6/6.
 6 //  Copyright (c) 2015年 小码哥. All rights reserved.
 7 //
 8 
 9 #import "XMGLoadMoreFooter.h"
10 #import "XMGDealsViewController.h"
11 
12 @interface XMGLoadMoreFooter()
13 @property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
14 @property (weak, nonatomic) IBOutlet UIView *loadingMoreView;
15 @end
16 
17 @implementation XMGLoadMoreFooter
18 
19 + (instancetype)footer
20 {
21     return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
22 }
23 
24 /**
25  * 点击了加载更多
26  */
27 - (IBAction)loadMore {
28     self.loadMoreButton.hidden = YES;
29     self.loadingMoreView.hidden = NO;
30     
31     // 告诉代理,非必须实现的代理方法先判断下是否被代理类实现,
32 //在内部发生某些行为时调用代理对应的代理方法,通知代理发生了什么事
33     if ([self.delegate respondsToSelector:@selector(loadMoreFooterDidClickLoadMoreButton:)]) {
34         [self.delegate loadMoreFooterDidClickLoadMoreButton:self];
35     }
36 }
37 
38 /**
39  * 结束加载状态
40  */
41 - (void)endLoading
42 {
43     self.loadMoreButton.hidden = NO;
44     self.loadingMoreView.hidden = YES;
45 }
46 
47 @end
m文件

 

  1 //
  2 //  XMGDealsViewController.m
  3 //  06-自定义等高cell01-storyboard
  4 //
  5 //  Created by xiaomage on 15/6/2.
  6 //  Copyright (c) 2015年 小码哥. All rights reserved.
  7 //
  8 
  9 #import "XMGDealsViewController.h"
 10 #import "XMGDeal.h"
 11 #import "XMGDealCell.h"
 12 #import "XMGPageView.h"
 13 #import "XMGLoadMoreFooter.h"
 14 
 15 @interface XMGDealsViewController () <XMGLoadMoreFooterDelegate>
 16 /** 所有的团购数据 */
 17 @property (nonatomic, strong) NSMutableArray *deals;
 18 @end
 19 
 20 @implementation XMGDealsViewController
 21 
 22 /**
 23     * 代理设计模式的作用:
 24     * 1.A对象监听B对象的一些行为,A成为B的代理
 25     * 2.B对象想告诉A对象一些事情,A成为B的代理
 26     *
 27     * 代理设计模式的总结:
 28     * 如果你想监听别人的一些行为,那么你就要成为别人的代理
 29     * 如果你想告诉别人一些事情,那么就让别人成为你的代理
 30     * 
 31     * 代理设计模式的开发步骤
 32     * 1.拟一份协议(协议名字的格式:控件名 + Delegate),在协议里面声明一些代理方法(一般代理方法都是@optional)
 33     * 2.声明一个代理属性:@property (nonatomic, weak) id<代理协议> delegate;
 34     * 3.在内部发生某些行为时,调用代理对应的代理方法,通知代理内部发生什么事
 35     * 4.设置代理:xxx.delegate = yyy;
 36     * 5.yyy对象遵守协议,实现代理方法
 37  */
 38 
 39 - (NSMutableArray *)deals
 40 {
 41     if (_deals == nil) {
 42         // 加载plist中的字典数组
 43         NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
 44         NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
 45         
 46         // 字典数组 -> 模型数组
 47         NSMutableArray *dealArray = [NSMutableArray array];
 48         for (NSDictionary *dict in dictArray) {
 49             XMGDeal *deal = [XMGDeal dealWithDict:dict];
 50             [dealArray addObject:deal];
 51         }
 52         
 53         _deals = dealArray;
 54     }
 55     return _deals;
 56 }
 57 
 58 
 59 - (void)viewDidLoad {
 60     [super viewDidLoad];
 61     
 62     // 设计模式:代理设计模式
 63     
 64     XMGLoadMoreFooter *footer = [XMGLoadMoreFooter footer];
 65     footer.delegate = self;
 66     self.tableView.tableFooterView = footer;
 67     
 68     XMGPageView *pageView = [XMGPageView pageView];
 69     pageView.imageNames = @[@"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2"];
 70     self.tableView.tableHeaderView = pageView;
 71 }
 72 
 73 #pragma mark - <XMGLoadMoreFooterDelegate>
 74 - (void)loadMoreFooterDidClickLoadMoreButton:(XMGLoadMoreFooter *)footer
 75 {
 76     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 77         // 加载数据
 78         XMGDeal *deal = [[XMGDeal alloc] init];
 79         deal.icon = @"2c97690e72365e38e3e2a95b934b8dd2";
 80         deal.title = @"xxxx";
 81         deal.price = @"6546";
 82         deal.buyCount = @"90";
 83         [self.deals addObject:deal];
 84         
 85         // 刷新表格
 86         [self.tableView reloadData];
 87         
 88         // 结束footer的加载状态
 89         XMGLoadMoreFooter *footer = (XMGLoadMoreFooter *)self.tableView.tableFooterView;
 90         [footer endLoading];
 91     });
 92 }
 93 
 94 #pragma mark - Table view data source
 95 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 96     return self.deals.count;
 97 }
 98 
 99 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
100     // 创建cell
101     XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
102     
103     // 取出模型数据
104     cell.deal = self.deals[indexPath.row];
105     
106     return cell;
107 }
108 
109 @end
实现代理

 

posted @ 2016-07-12 12:48  lwx64397  阅读(212)  评论(0编辑  收藏  举报