block页面之间简单传值
2015-09-30 23:53
183 查看
看一下效果图:
![](http://img.blog.csdn.net/20150930234726964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
// // ViewController.h // Block // // Created by LiuMingchuan on 15/9/29. // Copyright © 2015年 LMC. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController - (IBAction)btnToSecond:(id)sender; @property (strong, nonatomic) IBOutlet UILabel *infoLabel; @end
// // ViewController.m // Block // // Created by LiuMingchuan on 15/9/29. // Copyright © 2015年 LMC. All rights reserved. // #import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.infoLabel.lineBreakMode = NSLineBreakByWordWrapping; self.infoLabel.numberOfLines = 0; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)btnToSecond:(id)sender { UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; SecondViewController *sec = [storyBoard instantiateViewControllerWithIdentifier:@"second"]; SetInfoBlock setInfoBlock = ^(NSString *info){//创建SetInfoBlock类型的block self.infoLabel.text = info;//block体的作用就是将传入block的文字列赋值给我们第一个视图的UILabel的text属性 }; //设定secondViewController的block [sec setInfo:setInfoBlock]; [sec setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; [self presentViewController:sec animated:YES completion:nil]; } @end
// // SecondViewController.h // Block // // Created by LiuMingchuan on 15/9/30. // Copyright © 2015年 LMC. All rights reserved. // #import <UIKit/UIKit.h> /** * Block的定义 * * @param info 文字列形参 */ typedef void(^SetInfoBlock)(NSString *info); @interface SecondViewController : UIViewController - (IBAction)backToOneBtn:(id)sender; @property (strong, nonatomic) IBOutlet UITextView *inputText; /** * 自定义block的声明 */ @property (nonatomic,copy) SetInfoBlock setInfoBlock; /** * 用来设定本Controller的block * * @param setInfoBlock <#setInfoBlock description#> */ - (void)setInfo:(SetInfoBlock)setInfoBlock; @end
// // SecondViewController.m // Block // // Created by LiuMingchuan on 15/9/30. // Copyright © 2015年 LMC. All rights reserved. // #import "SecondViewController.h" #import "ViewController.h" @implementation SecondViewController - (IBAction)backToOneBtn:(id)sender { //当我们的block不为nil时进行处理 if (self.setInfoBlock != nil) { //将我们输入的内容传入从第一个视图传进来的block中 self.setInfoBlock(self.inputText.text); } [self dismissViewControllerAnimated:YES completion:nil]; } /** * 接受外部传入的block * * @param setInfoBlock 传入的block */ - (void)setInfo:(SetInfoBlock)setInfoBlock{ self.setInfoBlock = setInfoBlock; } @end
相关文章推荐
- Ubuntu 安装 jdk1.7,tomcat7
- CString作为参数传递的大问题
- 360笔试题二:二元选择思维
- JS原生Date类型方法
- JAVA循环结构
- HDU 5490 Simple Matrix (2015合肥站网络赛1007 数学题)
- DOM中Property与Attribute的区别
- LeetCode #4 Median of Two Sorted Arrays (H)
- 剑指offer—包含min函数的栈
- word search II
- RestFul && HATEOAS && Spring-Data-Rest介绍
- JVM监控与调优
- 为什么看起来不是很复杂的网站,淘宝、腾讯却需要大量顶尖高手来开发?
- PAT
- 未来的你肯定会感谢现在努力的你
- 电商网站如何推广自己呢?
- asp.net web forms page life cycle
- 剑指offer—顺时针打印矩阵
- c# Nested transactions are not supported
- UVA 11552 Fewest Flops(区间dp)