Block(块或者闭包)实现页面传值

      在其他语言里,我们经常会听到闭包,或者蓝布他表达式的概念,Block作为C语言的扩展,并不是高新技术,和其他语言的闭包或lambda表达式是一回事。Block的使用很像函数指针,不过与函数最大的不同是:Block可以访问函数以外、词法作用域以内的外部变量的值。换句话说,Block不仅实现函数的功能,还能携带函数的上下文。
这里我理解类似于Block,在oc里面,一般称为:块。
闭包的特点,就是可以将代码像对象一样传递,令其在不同环境(context)下运行。而且在块的范围内,它可以访问到其中的全部变量。

使用Block进行页面的传值,只需要在需要的时候,回调Block就可以。这就是Block的神奇之处。

要实现的功能:第一个页面接收第二个页面传来的参数。
如果红色部分注释掉,就可以使用蓝色部分。可以同样实现传值的功能,只是为了易于理解。
页面一:

#import "EVNFirViewController.h"

@interface EVNFirViewController ()

@end


@implementation EVNFirViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.navigationItem.title = @"EVNFirView";

    [self.view setBackgroundColor:[UIColor whiteColor]];

    

    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(goNext:)];

    self.navigationItem.rightBarButtonItem = rightBarBtn;

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回Fir" style:UIBarButtonItemStylePlain target:nil action:nil];

    

    

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 70)];


    self.label.textColor = [UIColor redColor];

    self.label.backgroundColor = [UIColor grayColor];

    [self.view addSubview:self.label];

}


- (void) goNext:(UIBarButtonItem *)sender

{

    EVNSecViewController *sec = [[EVNSecViewController alloc]init];

    

   

    sec.myblock = ^(NSString *str){

        self.label.text = str;

    };


    [sec tranData:^(NSString *str) {

        self.label.text = str;

        NSLog(@"str is value:%@",str);

    }];

    [self.navigationController pushViewController: sec animated:YES];

    

}

@end


页面二:

#import <UIKit/UIKit.h>

#import "EVNSecViewController.h"


typedef void (^MyBlock)(NSString *str);


@interface EVNSecViewController : UIViewController

@property (copy, nonatomic) MyBlock myblock;

-(void)tranData:(MyBlock)block;//这个地方不是必须这样,只是为了容易理解。注意蓝色部分。


@end


#import "EVNSecViewController.h"

@interface EVNSecViewController ()

@end

@implementation EVNSecViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.navigationItem.title = @"SecView";

    [self.view setBackgroundColor:[UIColor brownColor]];

    UIBarButtonItem *rightBarbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(BackFirView:)];

    self.navigationItem.rightBarButtonItem = rightBarbtn;

}

- (void) BackFirView:(UIBarButtonItem *)sender

{  

    if (self.myblock !=nil)

    {

        self.myblock(@"asdfasdf");

    }

    [self.navigationController popToRootViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

-(void)tranData:(MyBlock)block

{

    self.myblock = block;   

}

/**********

-(void)viewWillDisappear:(BOOL)animated

{

    if (self.myblock !=nil)

    {

        self.myblock(@"asdfasdf");

    }

}

**********/

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值