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;
}
那么参数就回传成功
上一篇: java 加载Https证书
下一篇: Linux 信号量