代理的使用步骤
定义一份代理协议
- 协议名字的格式一般是:类名 + Delegate
- 比如UITableViewDelegate
- 设计代理的细节
- 一般都是@optional(让代理可以有选择性去实现一些代理方法)
- 方法名一般都以类名开头
- 比如
- (void)scrollViewDidScroll:
- 比如
- 一般都需要将对象本身传出去
- 比如tableView的代理方法都会把tableView本身传出去
- 必须要遵守NSObject协议(基协议)
- 比如
@protocol AZWineCellDelegate <NSObject>
- 比如
- 协议名字的格式一般是:类名 + Delegate
声明一个代理属性
- 代理的类型格式:id<协议> delegate
@class AZWine,AZWineCell;
@protocol AZWineCellDelegate <NSObject>
@optional
/*增加按钮的点击*/
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell;
/*减号按钮的点击*/
-(void)wineCellDidClickMinusButton:(AZWineCell *)cell;
@end
@interface AZWineCell : UITableViewCell
/*模型*/
@property(nonatomic,strong)AZWine *wine;
/*设置代理*/
@property(nonatomic, weak) id<AZWineCellDelegate> delegate;
@end
- 设置代理对象
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 绑定标识
static NSString *ID=@"wine";
// 从缓存池中读取cell
AZWineCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
// 设定数据
cell.wine=self.wineArray[indexPath.row];
// 绑定代理
cell.delegate=self;
return cell;
}
- 代理对象遵守协议,实现协议里面相应的方法
- 遵守协议
@interface ViewController ()<UITableViewDataSource,AZWineCellDelegate,UITableViewDelegate>
- 实现对应的方法
- (void)wineCellDidClickPlusButton:(AZWineCell *)cell
- 遵守协议
- 当控件内部发生了一些事情,就可以调用代理的代理方法通知代理
- 如果代理方法是@optional,那么需要判断方法是否有实现,直接调用可能会报错
if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
[self.delegate wineCellDidClickPlusButton:self];
}
iOS通知的基本使用
- 发布通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"minusBtnClickNotification" object:self userInfo:nil];
- 设置监听
-(void)viewDidLoad {
[super viewDidLoad];
// 监听通知
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(plusBtnClick:) name:@"plusBtnClickNotification" object:nil];
[center addObserver:self selector:@selector(minusBtnClick:) name:@"minusBtnClickNotification" object:nil];
}
- 实现方法
-(void)minusBtnClick:(NSNotification *)note
KVO的基本使用
- 实现对AZWine模型内value(num)值变化的监听
-(NSMutableArray *)wineArray
{
if (!_wineArray) {
// 获得路径
NSString *path=[[NSBundle mainBundle]pathForResource:@"wine.plist" ofType:nil];
// 获得数组
NSArray *array=[NSArray arrayWithContentsOfFile:path];
// 创建一个临时数组存放模型数据
NSMutableArray *tempArray=[NSMutableArray array];
// 添加模型
for (NSDictionary *dict in array) {
//字典转模型
AZWine *wine=[AZWine wineWithDict:dict];
// 实现对模型value值变化的监听
[wine addObserver:self forKeyPath:@"num" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
[tempArray addObject:wine];
}
_wineArray=tempArray;
}
return _wineArray;;
}
- 当监听到KeyPath发生改变时调用方法
/**
* 当监听到模型value值变化时来到这个方法
*
* @param keyPath
* @param wine 监听的内容
* @param change 变化前后的值 new old
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject: (AZWine *)wine change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
// NSKeyValueChangeNewKey==@"new"
int new=[change[NSKeyValueChangeNewKey] intValue];
//NSKeyValueChangeOldKey==@"old"
int old=[change[NSKeyValueChangeOldKey] intValue];
...
}
iOS监听某些事件的方法
- 通知(NSNotificationCenter\NSNotification)
- 任何对象之间都可以传递消息
- 使用范围
- 1个对象可以发通知给多个对象
- 1个对象可以接受多个对象发出的通知
- 要求:必须得保证通知的名字在发出和监听时是一致的
- KVO
- 仅仅是能监听对象属性的改变(灵活度不如通知和代理)
代理
- 使用范围
- 1个对象只能设置一个代理(假设这个对象只有1个代理属性)
- 1个对象能成为多个对象的代理
- 使用范围
如何选择?
代理
比通知
规范- 建议使用
代理
多于通知
,能使用代理尽量使用代理