iOS Block 页面传值

时间:2023-03-09 18:49:37
iOS Block 页面传值

直接上代码

1.定义block

@interface TopTypeCollectionView : UIView

@property (nonatomic, strong) NSArray *typeItemArray;//数据源

typedef void(^Blo)(NSString *s1 , UIColor *c);//定义block名字
@property (nonatomic , copy) Blo block;//声明 /**
cell点击回调Block
*/
@property (nonatomic , copy) void (^clickCellCb)(NSString *group,int index);//匿名block @end

2.TopTypeCollectionView中调用

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//调用block
if (self.clickCellCb) {
self.clickCellCb(self.group , (int)indexPath.row);
}
}

3.初始化block

__weak  ViewController *weakself = self;
//类型列表
TopTypeCollectionView *topTypeView = [[TopTypeCollectionView alloc] initWithFrame:CGRectZero];
topTypeView.typeItemArray = @[];
topTypeView.backgroundColor = [UIColor whiteColor];
topTypeView.group = group;
//点击cell,打开页面
topTypeView.clickCellCb = ^(NSString *groupTemp , int index){
[weakself openWallpaperView:groupTemp index:index];
};

转载于:https://my.oschina.net/huqiji/blog/834205