代理设计模式的作用:
     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. 无法找到.exe的调试信息

    原文:无法找到.exe的调试信息 前天重装了电脑,并配置了Visual Studio2005的VC正则库,boost/regex,运行速度马上快了三倍吧,到底是怎么快的,我还真说不清楚,因为电脑这玩意 ...

  2. WebDriver打开浏览器-java

    环境:配置jdk.使用Eclipse(个人爱好),导入selenium-java-2.42.2.jar.selenium-java-2.42.2-srcs.jar.selenium-server-st ...

  3. js实现收缩菜单效果

    废话不多说,直接上代码: 有注释 <head> <title></title> <style type="text/css"> di ...

  4. 文件I/O操作(1)

    linux系统调用和用户编程接口(api) 系统调用是指在操作系统提供给用户程序调用的一组“特殊”的接口,用户程序可以通过这组特殊的接口来获取操作系统内核提供的服务,例如用户可以通过进程控制相关的系统 ...

  5. Inf2Cat, signability test failed.

    驱动开发真不是那么好玩的,折腾了几天,排除了几个错误,又有新的错误了. 错误    1    error -2: "Inf2Cat, signability test failed.&quo ...

  6. input文字方框中,字体颜色的变化 要求默认的字体颜色是灰色,当要输入字时,字体是正常的黑色

    1 <input type=text name='address' size=60 maxlength=60 style="color:gray" value="( ...

  7. centos7 rabbitmq系统部署

    CentOS7 1.安装系统 基础设施服务器:Java平台.Linux远程管理.开发工具 2.打开网络连接: (1)cd  /etc/sysconfig/network-scripts/  #进入网络 ...

  8. 【Zookeeper学习】Apache Zookeeper项目简介

    正在撰写,稍后来访……

  9. 解决网站出错后 跳转 友好页面 的 asp .net 配置

    <system.webServer> <httpErrors errorMode="DetailedLocalOnly"> <remove statu ...

  10. 远程测试mysql数据库3306端口报错

    错误现象:[root@localhost ~]# telnet 192.168.10.130 3306Trying 192.168.10.130...Connected to 192.168.10.1 ...