Delegate、Block、Notification

2016-12-28  本文已影响55人  DDY
对比
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) {
};
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;

任务完成时回调处理
消息监听回调处理
错误回调处理
枚举回调
视图动画、变换
排序

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传值、代理传值与通知中心传值

上一篇 下一篇

猜你喜欢

热点阅读