Block--两个界面 间回调传值
2016-01-13 14:30
274 查看
使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值:
首先,创建两个视图控制器,在第一个视图控制器中创建一个UILabel和一个UIButton,其中UILabel是为了显示第二个视图控制器传过来的字符串,UIButton是为了push到第二个界面。
第二个视图控制器
第一个视图控制器
首先,创建两个视图控制器,在第一个视图控制器中创建一个UILabel和一个UIButton,其中UILabel是为了显示第二个视图控制器传过来的字符串,UIButton是为了push到第二个界面。
第二个视图控制器
#import <UIKit/UIKit.h> typedef void (^PassValueBlock) (NSString *ShowText); @interface DetailViewController : UIViewController @property (nonatomic,copy) PassValueBlock ShowTextBlock; -(void)ShowTextValue:(PassValueBlock)block; @end
#import "DetailViewController.h" @interface DetailViewController () @end @implementation DetailViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor=[UIColor blueColor]; button.frame=CGRectMake(100, 100, 100, 20); [button setTitle:@"回调传值" forState:UIControlStateNormal]; [button addTarget:self action:@selector(PassValue:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)PassValue:(UIButton *)sender{ [self.navigationController popViewControllerAnimated:YES]; //block 传值 if (self.ShowTextBlock != nil) { self.ShowTextBlock(@"20"); } } -(void)ShowTextValue:(PassValueBlock)block{ //保存block self.ShowTextBlock = block; }
第一个视图控制器
#import "RootViewController.h" #import "DetailViewController.h" @interface RootViewController () @property (nonatomic,strong) UILabel *label; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; //创建显示数据的label self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 40, 60)]; self.label.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.label]; //创建button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor blueColor]; button.frame = CGRectMake(100, 100, 40, 20); [button addTarget:self action:@selector(Push:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)Push:(UIButton *)sender{ //跳转控制器 DetailViewController *detailVC = [[DetailViewController alloc] init]; [self.navigationController pushViewController:detailVC animated:YES]; //回调传值 [detailVC ShowTextValue:^(NSString *ShowText) { self.label.text = ShowText; }]; }
相关文章推荐
- winform异型不规则界面设计的实现方法
- Android中设置只有程序第一次运行才显示的界面实现思路
- hta 实现的五子棋界面
- android开发之欢迎界面的小例子
- C#中载入界面的常用方法
- android编程实现局部界面动态切换的方法
- Android判断现在所处界面是否为home主桌面的方法
- Android编程实现泡泡聊天界面实例详解(附源码)
- android 引导界面的实现方法
- VC 界面库皮肤库相关信息珍藏
- Android中界面实现全屏显示的两种方式
- 简单做出不丑设计
- extmail 界面修改
- QT程序启动界面的使用
- The Book of Qt 4 翻译: 2.2 界面和处理逻辑的分离
- 软件界面交互和易用性改进总结
- WP7界面设计与交互指南中英译本(附下载)
- 微软发布Windows Phone 7技术预览版(多图)(1)
- wxpython笔记
- 修改SecureCRT终端的Home和End功能键