Block的简单使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/M_15915899719/article/details/82761078

两个视图之间的回调实例,一般处理的方法有代理、通知、block,这里简单描述下block的简单使用。

@interface LMFirstCell : UICollectionViewCell

// block返回字符串
// 声明一个block
typedef void(^MyBlock)(NSString *string);
// 定义一个block属性
@property (nonatomic, copy) MyBlock myBlock;

.m文件里

- (void)tapClick {
// 这里设置需要回传单的参数
    if (self.myBlock) {
        self.myBlock(@"myBlock:被点击了!");
    }
}

在另一个视图或者控制器里面调用block

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LMFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.myBlock = ^(NSString *string) {
        NSLog(@"cell:%@",string);
    };
    return cell;
}

那么参数就回传成功

猜你喜欢

转载自blog.csdn.net/M_15915899719/article/details/82761078
今日推荐