欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Block的简单使用

程序员文章站 2022-07-13 16:44:17
...

两个视图之间的回调实例,一般处理的方法有代理、通知、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;
}

那么参数就回传成功

相关标签: 移动开发