标签:block传值
代码:
[self.view setBackgroundColor:[UIColor redColor]];
代码:
self.navigationController.navigationBar.translucent=NO;
// 创建 button
UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(100, 100, 100, 30);
[button setBackgroundColor:[UIColor redColor]];
button.layer.borderWidth=1;
button.layer.cornerRadius=10;
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
代码:
-(void)buttonClick:(UIButton *)button{
通过block实现从后向前传值
block传值不需要返回值,因为调用更需要返回值,所以传值的时候只要参数,不需要返回值
void (^block)(NSString *)=^(NSString *str){
NSLog(@"%@",str );
};
void (^blockArr)(NSArray *)=^(NSArray *arr){
NSLog(@"%@",arr);
};
传一个字符串和一个数组
跳到第二页面
SecondViewController *sec=[[SecondViewController alloc] init];
[self.navigationController pushViewController:sec animated:YES];
[sec release];
代码:
@property(nonatomic ,copy)void (^block)(NSString *);
@property(nonatomic ,copy)void (^blockArr)(NSArray *);
代码:
2.通过第二页的属性接收block
sec.block=block;
sec.blockArr =blockArr;
代码:
-(void)buttonClick:(UIButton *)button{
// 3.调用传过来的block
self.block(@"刘山山");
self.blockArr(@[@"1",@"2",@"3"]);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:block传值
原文地址:http://blog.csdn.net/mltianya/article/details/47612921