36.Block页面间传值

来源:互联网 发布:php清除cookie 编辑:程序博客网 时间:2024/06/11 04:42

1.基本概念

  1. block是匿名函数, 能够实现函数回调功能, 用于页面之间通信和传值.
  2. 定义属性接收block必须使用copy修饰, retain和assign会造成野指针问题.
  3. block在某个方法中定义是存储在栈区, 在另一个雷中使用需要进行copy, 存储在堆区.
  4. 当不使用block时需要用自己的有的方法销毁, Block_Release()
  5. 在block实现部分, 不能直接使用实例变量, self调用属性, 因为block会造成self引用计数加1, 最终导致循引用问题. 使用__block解决循环引用的问题.

2.简单举例

MainViewController.m文件

#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()@end@implementation MainViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor cyanColor];    self.navigationController.navigationBar.translucent = NO;    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(100, 300, 200, 49);    button.layer.borderWidth = 1;    button.layer.cornerRadius = 10;    [button setTitle:@"下一页" forState:UIControlStateNormal];    [self.view addSubview:button];    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}- (void)click:(UIButton *)button{    SecondViewController *secondVC = [[SecondViewController alloc] init];    //没有参数,没有返回值的block,通过block,改变self.view的背景颜色    void (^myBlock)() = ^(){     self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];    };    //2.通过第二页的属性来接收block    secondVC.myBlock = myBlock;    //通过block实现从后向前传值,不需要返回值,因为调用更需要返回值,所以传值的时候只要参数    void (^myblock)(NSString *) = ^(NSString *str){        NSLog(@"%@",str);    };    secondVC.myblock = myblock;    void (^block)(NSArray *) = ^(NSArray *arr){        //传过来的arr的有效范围就这对大括号里,所以穿过来的数据处理都在block里面        NSLog(@"%@",arr);    };    secondVC.block = block;    [self.navigationController pushViewController:secondVC animated:YES];    [secondVC release];}

SecondViewController.h和SecondViewController.m文件

#import <UIKit/UIKit.h>typedef void(^Block)(NSArray *);@interface SecondViewController : UIViewController//1.写一条属性,负责接受第一页向第二页传递过来的block@property(nonatomic,copy)void(^myBlock)();@property(nonatomic,copy)void(^myblock)(NSString *);@property(nonatomic,copy)Block block;//block作为属性时为了防止block进入到栈区,栈区内存不需要我们进行管理,很可能出现block消失的情况,所以需要拷贝一份到堆区@end
#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)dealloc{    //这是block自己的release的方法    Block_release(_myBlock);    Block_release(_myblock);    Block_release(_block);    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor cyanColor];    self.navigationController.navigationBar.translucent = NO;    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(100, 300, 200, 49);    button.layer.borderWidth = 1;    button.layer.cornerRadius = 10;    [button setTitle:@"返回" forState:UIControlStateNormal];    [self.view addSubview:button];    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}- (void)click:(UIButton *)button{    //3.调用传过来的block    self.myBlock();    self.myblock(@"网易新闻");    NSArray *arr = @[@"zhangsan",@"lishi"];    self.block(arr);    [self.navigationController popToRootViewControllerAnimated:YES];}
0 0
原创粉丝点击