Delegate、Block、Notification
对比
-
Delegate(委托/代理)
优点:
1.同一个协议,一个对象只能同时设置一个代理,单例一般不用代理;
2.相对侧重于过程;
3.降低代码的耦合性,事件监听和处理相对分离。
缺点:
1.实现过程繁琐;
2.跨层传值监听不方便;
3.难以对多个对象同时传值。 -
Block(块)
优点:
1.书写简练;
2.侧重结果;
3.配合GCD解决多线程问题。
缺点:
1.需要防止循环引用;
2.delegate运行成本低,block成本很高的。
block出栈需要将使用的数据从栈内存拷贝到堆内存,当然对象的话就是加计数,使用完或者block置nil后才消除;delegate只是保存了一个对象指针,直接回调,没有额外消耗。相对C的函数指针,只多做了一个查表动作 -
Notification(通知)
优点:
1.一对多,解决了多对象监听问题;
2.传值方便快捷,Context自身携带相应的内容。
缺点:
1..使用完要记得注销通知,防止crash;
2.调试的时候动作的跟踪将很难进行;
3.需要一个第三方的对象来做监听者与被监听者的中介。
Delegate
//协议文件中,值提供方法的声明,不提供具体的实现内容
//谁遵循了该协议,谁来实现协议中声明的方法
//协议本身可以遵循其他协议
//默认情况下所有的协议,都遵循了NSObject基础协议
@class NAEvaluateCell;
@protocol NAEvaluateCellDelegate <NSObject>
// 可选实现标记
@optional
- (void)addPictures;
// 必选实现标记,语法级别警告,并不会导致程序编译错误
@required
- (void)evaluateCell:(NAEvaluateCell *)cell deleteIndex:(NSInteger)index;
@end
一些注意点:
1 做判断
if (self.delegate && [self.delegate respondsToSelector:@selector(evaluateCell:deleteIndex:)]) {
[self.delegate evaluateCell:weakSelf deleteIndex:index];
}
2 遵循协议<NAEvaluateCellDelegate>
3 实现方法
Block
typedef void (^deleteData)(int index);
@interface NAEvaluateItemCell : UICollectionViewCell
/** 宏定义方式 */
@property (nonatomic, copy) deleteData deleteDataIndex;
/** 直接书写 */
@property (nonatomic, copy) void (^clickBlock)(void); // 无参数无返回值
@end
// 使用
self.clickBlock();
[cell setClickBlock:^{ }];
if (self.deleteDataIndex) {
self.deleteDataIndex((int)self.indexPath.row);
}
cell.deleteDataIndex = ^(int index) {
};
- Block模式
1.无参数无返回值
void (^emptyBlock)() = ^(){
NSLog(@"无参数无返回值");
};
emptyBlock();
2.有参数无返回值
void (^sumBlock)(int, int) = ^(int a, int b){
NSLog(@"有参数无返回值");
};
sumBlock(10,10);
3.有参数有返回值
NSString* (^logBlock)(NSString *, NSString *) = ^(NSString *str1, NSString *str2){
return [NSString stringWithFormat:@"%@%@",str1,str2];
};
NSLog(@"%@", logBlock(@"有参数有返回值的", @"Block"));
- 循环引用解决方案
ARC
如果用copy修饰Block,该Block就会存储在堆空间。则会对Block的内部对象进行强引用,导致循环引用,内存无法释放
__weak typeof (target)weakTarget = target;
如果用weak修饰Block,该Block就会存放在栈空间。不会出现循环引用问题
MRC
用copy修饰后要在Block内部使用对象,则需要__block typeof (target)blockTarget = target;
- 使用场合。
任务完成时回调处理
消息监听回调处理
错误回调处理
枚举回调
视图动画、变换
排序
- 主线程中UI操作
dispatch_async(dispatch_get_main_queue(), <^(void)block>)
Notification
- (void)addNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGFloat height = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[UIView animateWithDuration:0.2 animations:^{
} completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
}
#pragma mark 销毁时移除
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
根据isa指针,block一共有3种类型的block
_NSConcreteGlobalBlock 全局静态
_NSConcreteStackBlock 保存在栈中,出函数作用域就销毁
_NSConcreteMallocBlock 保存在堆中,retainCount == 0销毁
重要经验五:block作为属性的注意事项
iOS中Block的基础用法
知乎上block问题回答
Block,Delegate,Notification
浅谈 iOS Notification
iOS Block底层实现原理详解
Block全面分析
单例传值、block传值、代理传值与通知中心传值