36.Block页面间传值
来源:互联网 发布:php清除cookie 编辑:程序博客网 时间:2024/06/11 04:42
1.基本概念
- block是匿名函数, 能够实现函数回调功能, 用于页面之间通信和传值.
- 定义属性接收block必须使用copy修饰, retain和assign会造成野指针问题.
- block在某个方法中定义是存储在栈区, 在另一个雷中使用需要进行copy, 存储在堆区.
- 当不使用block时需要用自己的有的方法销毁, Block_Release()
- 在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
- 36.Block页面间传值
- UI之页面间传值(block传值)
- iOS开发使用Block页面间传值
- block 页面传值
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值方式(代理Delegate,通知Notification,块Block)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault
- iOS开发:通过block在两个页面间传值
- Block实现页面传值
- block块页面传值
- 关于Block页面传值
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- 【三分】Expanding Rods
- HDU 1495 非常可乐
- uva 120Stacks of Flapjacks
- 知乎-Android自动化测试工具总结
- UVA11401 Triangle Counting(递推,dp)
- 36.Block页面间传值
- TCP/IP、Http、Socket的区别
- ios开发之手势动作状态细分state,同一视图添加两个手势
- 大数据网站积累
- C与C++区别之处
- 机器学习漫谈
- NYOJ 38 布线问题(prim算法)
- HD1301 Jungle Roads 【Kruska】
- 作业