一:Detegate
代理delegate就是委托另一个对象来帮忙完成一件事情。
View类型代理
步骤:
- 定义: .h文件里:(@protocol xxxDelegate <NSObject>)
要实现的方法:-(void)didxxxx;
-属性:(@property(nonatomic,assign)id<xxxDelegate>delegate;)
示例:
HomeHeaderView.h文件
@class HomeHeaderView
@protocol HomeHeaderViewDelegate <NSObject>
-(void)didHeaderButtonWithTag:(NSInteger)tag;
@end
@interface HomeHeaderView : UIView
@property(nonatomic,assign)id<HomeHeaderViewDelegate>delegate;
@end
- HomeHeaderView.m文件里:触发响应方法:
#pragma mark -- Btn Action
-(void)didbuttonthings:(UIButton *)button
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(didHeaderButtonWithTag:)]) {
[_delegate didHeaderButtonWithTag:button.tag];
}
}
3.ViewController内遵守并实现delegate:
@interface ViewController ()<HomeHeaderViewDelegate>
_headerView.delegate=self;
#pragma mark- HomeHeaderViewDelegate
-(void)didHeaderImageWithTag:(NSInteger)tag{
}
Cell类型代理
QYMenuTableViewCell.h文件
@class QYMenuTableViewCell;
@protocol QYMenuTableViewCellDelegate <NSObject>
-(void)customBtnClicked:(NSInteger)index;
@end
@interface QYMenuTableViewCell : UITableViewCell
@property(nonatomic,assign)id<QYMenuTableViewCellDelegate>delegate;
@property(nonatomic,assign)NSInteger index;//记录点击cell的行数
@property(nonatomic,strong)UILabel *textLab;
@end
QYMenuTableViewCell.m文件
-(void)btnCick{
if (_delegate != nil && [_delegate respondsToSelector:@selector(customBtnClicked:)]) {
[_delegate customBtnClicked:_index];
}
}
Controller文件实现代理方法
@interface QYMenuViewController ()
<UITableViewDataSource,UITableViewDelegate,
QYMenuTableViewCellDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QYMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYMenuTableViewCell" forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLab.text = self.titlesArray[indexPath.row];
cell.index = indexPath.row;
cell.delegate = self;
return cell;
}
#pragma mark - QYMenuTableViewCellDelegate
-(void)customBtnClicked:(NSInteger)index{
DDLogDebug(@"点击%ld行",(long)index);
}
二.Block :逆向传值
Block的定义:
返回值类型(block变量名)(形参列表)=(形参列表){
};
调用:
block变量名(实参);
Block的模式
1.无参数无返回值的Block2.有参数无返回值的Block3.有参数有返回值的Block
// Block的使用示例
// 1.Block作为变量(Xcode快捷键:inlineBlock)
// 2. Block作为属性(Xcode 快捷键:typedefBlock)
// 3、作为 OC 中的方法参数
// 4、Block回调
// __weak typeof(self) weakSelf = self;//防止循环引用
.h文件:
1、定义一个Block: (Xcode 快捷键:typedefBlock)
typedef void (^ChangeBlock)(NSString* stringName);
2、声明Block属性
@property (nonatomic,copy)ChangeBlock changeName;
3、.m文件:调用
self.changeName(self.nameTextField.text);
4、接收传值
__weak __typeof(self) weakSelf = self;
vc.change=^(NSString* stringName){
weakSelf.NameLabel.text=stringName;
};
三.通知:Notification
1、不传递参数, 最常用的一种
// 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti1" object:nil];
//监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti1) name:@"noti1" object:nil];
//调用方法
-(void)noti1{
NSLog(@"接收 不带参数的消息");
}
2、使用object 传递消息
//发通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti2" object:[NSString stringWithFormat:@"%@",btn.titleLabel.text]];
//监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti2:) name:@"noti2" object:nil];
//调用方法
-(void)noti2:(NSNotification *)noti{
//使用object处理消息
NSString *info = [noti object];
NSLog(@"接收 object传递的消息:%@",info);
}
3、使用userInfo 传递消息
//发通知
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"userInfo消息" forKey:@"param"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti3" object:nil userInfo:dic];
//监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti3:) name:@"noti3" object:nil];
//调用方法
-(void)noti3:(NSNotification *)noti{
//使用userInfo处理消息
NSDictionary *dic = [noti userInfo];
NSString *info = [dic objectForKey:@"param"];
NSLog(@"接收 userInfo传递的消息:%@",info);
}
最后,记得在发送通知消息的页面,在dealloc方法里面移除观察者。
-(void)dealloc{
// 移除当前所有通知
NSLog(@"移除了所有的通知");
[[NSNotificationCenter defaultCenter] removeObserver:self];
//移除名称为tongzhi的那个通知
// NSLog(@"移除了名称为tongzhi的通知");
// [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];
}
网友评论