block传值

 A页面: 
A.h文件无内容.
A.m文件:
 - (void)push
{
    UILabel *label1 = (UILabel *)[self.view viewWithTag:1001];
    TwoViewController *two = [[TwoViewController alloc]init];
    [self.navigationController pushViewController:two animated:YES];
    two.block = ^(NSString *str){    //block是TwoViewController类中的属性.
        label1.text = str;
    };
    [two release];
    [label1 release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 100, 30);
    button.backgroundColor = [UIColor redColor];
    
    [button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
    label.backgroundColor = [UIColor greenColor];
    
    label.tag = 1001;
    [self.view addSubview:button];
    [self.view addSubview:label];
    [label release];
}

B页面:
B.h文件:
typedef void (^aBlock)(NSString *str);
@interface TwoViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic, copy)aBlock block;
@end
B.m文件:
-(void)viewWillDisappear:(BOOL)animated
{
    NSString *str = [NSString stringWithFormat:@"我是B页面的值"];
       if (self.block) {
           block(str);
      }
}
当B页面返回A页面时会调用:
 
two.block = ^(NSString *str){  
        label1.text = str;
    };
这样"我是B页面的值"字符串就传到A页面了....
第一次创建TwoViewController对象时block里的值为空,固没有调用block函数..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值