代理设计模式的作用:
     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对象遵守协议,实现代理方法

 #import <UIKit/UIKit.h>

 @class XMGLoadMoreFooter;

 @protocol XMGLoadMoreFooterDelegate <NSObject>
@optional//不写@optional默认为必须实现的代理方法
//代理方法一般以类名开头,传入被监听的控件对象
- (void)loadMoreFooterDidClickLoadMoreButton:(XMGLoadMoreFooter *)footer;
@end @interface XMGLoadMoreFooter : UIView
+ (instancetype)footer;
//代理属性用weak修饰,id类型代表任何类都可以作为代理,在<>中指明要实现的协议名称
@property (nonatomic, weak) id<XMGLoadMoreFooterDelegate> delegate; /**
* 结束加载状态
*/
- (void)endLoading;
@end

h文件

 //
// XMGLoadMoreFooter.m
// 06-自定义等高cell01-storyboard
//
// Created by xiaomage on 15/6/6.
// Copyright (c) 2015年 小码哥. All rights reserved.
// #import "XMGLoadMoreFooter.h"
#import "XMGDealsViewController.h" @interface XMGLoadMoreFooter()
@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
@property (weak, nonatomic) IBOutlet UIView *loadingMoreView;
@end @implementation XMGLoadMoreFooter + (instancetype)footer
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
} /**
* 点击了加载更多
*/
- (IBAction)loadMore {
self.loadMoreButton.hidden = YES;
self.loadingMoreView.hidden = NO; // 告诉代理,非必须实现的代理方法先判断下是否被代理类实现,
//在内部发生某些行为时调用代理对应的代理方法,通知代理发生了什么事
if ([self.delegate respondsToSelector:@selector(loadMoreFooterDidClickLoadMoreButton:)]) {
[self.delegate loadMoreFooterDidClickLoadMoreButton:self];
}
} /**
* 结束加载状态
*/
- (void)endLoading
{
self.loadMoreButton.hidden = NO;
self.loadingMoreView.hidden = YES;
} @end

m文件

 //
// XMGDealsViewController.m
// 06-自定义等高cell01-storyboard
//
// Created by xiaomage on 15/6/2.
// Copyright (c) 2015年 小码哥. All rights reserved.
// #import "XMGDealsViewController.h"
#import "XMGDeal.h"
#import "XMGDealCell.h"
#import "XMGPageView.h"
#import "XMGLoadMoreFooter.h" @interface XMGDealsViewController () <XMGLoadMoreFooterDelegate>
/** 所有的团购数据 */
@property (nonatomic, strong) NSMutableArray *deals;
@end @implementation XMGDealsViewController /**
* 代理设计模式的作用:
* 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对象遵守协议,实现代理方法
*/ - (NSMutableArray *)deals
{
if (_deals == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组
NSMutableArray *dealArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
XMGDeal *deal = [XMGDeal dealWithDict:dict];
[dealArray addObject:deal];
} _deals = dealArray;
}
return _deals;
} - (void)viewDidLoad {
[super viewDidLoad]; // 设计模式:代理设计模式 XMGLoadMoreFooter *footer = [XMGLoadMoreFooter footer];
footer.delegate = self;
self.tableView.tableFooterView = footer; XMGPageView *pageView = [XMGPageView pageView];
pageView.imageNames = @[@"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2"];
self.tableView.tableHeaderView = pageView;
} #pragma mark - <XMGLoadMoreFooterDelegate>
- (void)loadMoreFooterDidClickLoadMoreButton:(XMGLoadMoreFooter *)footer
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 加载数据
XMGDeal *deal = [[XMGDeal alloc] init];
deal.icon = @"2c97690e72365e38e3e2a95b934b8dd2";
deal.title = @"xxxx";
deal.price = @"";
deal.buyCount = @"";
[self.deals addObject:deal]; // 刷新表格
[self.tableView reloadData]; // 结束footer的加载状态
XMGLoadMoreFooter *footer = (XMGLoadMoreFooter *)self.tableView.tableFooterView;
[footer endLoading];
});
} #pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.deals.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建cell
XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView]; // 取出模型数据
cell.deal = self.deals[indexPath.row]; return cell;
} @end

实现代理

ios代理设计模式的更多相关文章

  1. 你真的了解iOS代理设计模式吗?

    在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存管理等方面的知识.我会通过这些方面的知识,带大 ...

  2. 【转】你真的了解iOS代理设计模式吗?

    转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...

  3. iOS 代理设计模式

    在项目中经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式传递一些参数. 在项目中,刚开始我是用一些代理来传递参数的,但是慢慢觉得代理的代码比较block多,所以就更多的使用 ...

  4. IOS之Objective-C学习 代理设计模式

    鉴于Objective-C是不支持多继承的,所以需要用协议来代替实现其他类的方法,所以有了代理设计模式. 代理,又称委托,delegation. 代理模式可以让一个单继承的类实现父类以外其他类的方法. ...

  5. iOS常用设计模式和机制之代理

    Delegate : 1 代理设计模式的使用我们首先需要明白三个要素 *委托方:委托别人去执行某些操作的人(对象) *代理方:被委托区执行某些操作的人(对象) *协议:(protocol)委托方需要代 ...

  6. iOS 代理反向传值

    在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...

  7. 包建强的培训课程(8):iOS与设计模式

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. IOS的设计模式

    对象创建 原型(Prototype) 使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象. NSArray *array = [[NSArray alloc] initWithObject ...

  9. Objective-C之代理设计模式小实例

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. HDU 5675 ztr loves math

    ztr loves math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. 远程调试hadoop各组件

    远程调试对应用程序开发十分有用.例如,为不能托管开发平台的低端机器开发程序,或在专用的机器上(比如服务不能中断的 Web 服务器)调试程序.其他情况包括:运行在内存小或 CUP 性能低的设备上的 Ja ...

  3. The new Portable Class Library for SQLite z

    Microsoft Open Technologies has recently released a Portable Class Library for SQLite. Thanks to it, ...

  4. delphi 数据导出到word

    procedure TFrmWeekAnalysisQry.BtnExportToExcelClick(Sender: TObject);var wordApp,WordDoc,WrdSelectio ...

  5. js 数组常用方法说明

    //push 向数组最后添加一项 var arr = ['one', 'two', 'three']; arr.push("four"); console.log(arr);//[ ...

  6. hive内部表与外部表区别

    1.在Hive里面创建一个表: hive> create table wyp(id int,    > name string,    > age int,    > tele ...

  7. 跨平台的游戏客户端Socket封装,调整

    原文链接:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019359.html 头文件: #pragma once #ifdef WIN32 ...

  8. 将Sublime Text 2搭建成一个好用的IDE

    将Sublime Text 2搭建成一个好用的IDE 说起编辑器,可能大部分人要推荐的是Vim和Emacs,本人用过Vim,功能确实强大,但是不是很习惯,之前一直有朋友推荐SUblime Text 2 ...

  9. [置顶] Effective STL 学习笔记

    看Effective STL 作的一些笔记,希望对各位有帮助. 以下是50条条款及相关解释. 容器 1. 慎重选择容器类型,根据需要选择高效的容器类型. 2. 不要试图编写独立于容器类型的代码. 3. ...

  10. 使用 StoryBoard 的时候加入用户引导页面

    如果想让一个APP加上引导页面是一个非常完美的举动 但是,总会遇到一些问题 (不要忘记在APDelegate里面加上用户引导页面的头文件和程序启动时的第一个页面哦) 情况一:纯代码 判断是否是第一次启 ...