@interface QWViewController : UIViewController

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)UITextField *textField;

@end

#import "QWViewController.h"

#import "QWViewControllerTwo.h"

@interface QWViewController ()

@end

@implementation QWViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

label.backgroundColor=[UIColor blueColor];

self.label=label;

[self.view addSubview:label];

UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

textField.backgroundColor=[UIColor yellowColor];

self.textField=textField;

[self.view addSubview:textField];

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

button.frame=CGRectMake(100, 300, 100, 50);

button.backgroundColor=[UIColor blackColor];

[button setTitle:@"jump" forState:UIControlStateNormal];

[button addTarget:self action:@selector(doClicked) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

-(void)doClicked{

QWViewControllerTwo *ViewControllerTwo=[[QWViewControllerTwo alloc]init];

ViewControllerTwo.labelText=self.textField.text;//顺传值

//[ViewControllerTwo.label setText:self.textField.text];//注意:这样传值是不对的。

//逆向传值回来

[ViewControllerTwo returnText:^(NSString *showText) {

self.label.text=showText;

}];

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

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

#import <UIKit/UIKit.h>

typedef void(^ReturnTextBlock) (NSString *showText);

@interface QWViewControllerTwo : UIViewController

@property(nonatomic,copy)NSString *labelText;

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)UITextField *textField;

@property(nonatomic,copy)ReturnTextBlock returnTextBlock;

-(void)returnText:(ReturnTextBlock)block;

@end

#import "QWViewControllerTwo.h"

@interface QWViewControllerTwo ()

@end

@implementation QWViewControllerTwo

-(UILabel *)label{

if (_label==nil) {

_label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

}

return _label;

}

-(UITextField *)textField{

if (_textField==nil) {

_textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

}

return _textField;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

label.backgroundColor=[UIColor yellowColor];

label.text=self.labelText;

self.label=label;

[self.view addSubview:self.label];

UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

textField.backgroundColor=[UIColor yellowColor];

self.textField=textField;

[self.view addSubview:textField];

//创建返回按钮

UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];

[backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

[backBtn setTitle:@"back" forState:UIControlStateNormal];

}

-(void)goBack{

[self.navigationController popViewControllerAnimated:YES];

}

//将上一个控制器传过来的block保存在本控制器中,在合适的时候调用

-(void)returnText:(ReturnTextBlock)block{

self.returnTextBlock=block;

}

//在视图将要消失的时候调用本类的block

-(void)viewWillDisappear:(BOOL)animated{

if (self.returnTextBlock !=nil) {

self.returnTextBlock(self.textField.text);//实现是在之前控制器中

}

}

@end

实现效果:

IOS传值之Block传值(二)的更多相关文章

  1. IOS笔记047-代理传值和block传值

    在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...

  2. OS笔记047代理传值和block传值

    在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...

  3. 属性传值,协议传值,block传值,单例传值四种界面传值方式

    一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N ...

  4. iOS传值之block传值(一)

    ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...

  5. iOS中使用block传值

    转自:http://blog.sina.com.cn/s/blog_60b45f230100yiaf.html 用此方法传值可以替代委托了.具体例子: MainView.h #import <U ...

  6. iOS 页面间传值 之 单例传值 , block 传值

    ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...

  7. ios常见的页面传值方式

    iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...

  8. iOS的四种传值方式

    传值有四种方法 : 1. 属性传值 2. 单例传值 3. 代理传值 4. block传值     一.属性传值   (前-->后) 1. 后面的界面定义一个属性  存放前一个界面传过来的值    ...

  9. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

随机推荐

  1. 如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...

  2. 淘淘商城_day07_课堂笔记

    今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...

  3. HDU 5834 Magic boy Bi Luo with his excited tree

    树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...

  4. Swift逃逸闭包之见解

    Swift 逃匿闭包顾名思义,就是闭包想要逃跑.当闭包作为参数传给一个方法时,在这个方法被调用完后闭包却还没有被执行,而是等到方法执行完后才调用 基本都是跨线程的时候才会有逃逸闭包这个说法.因为异步 ...

  5. 1-jQuery - AJAX load() 方法【基础篇】

    jQuery load() 方法是简单但强大的 AJAX 方法:load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 格式 $(selector).load(URL 源码 index.h ...

  6. Google科学家前腾讯副总裁吴军将出席第二届万物互联创新大会

    当越来越多的科技产品注入互联网的基因,"万物互联"的模式悄然兴起.第二届万物互联创新大会(B12大会)将于2016-11-13日在杭州市余杭区隆重召开.Google科学家前腾讯副总 ...

  7. 抓取网页中数据 -----51book中城市码

    ================== 获取网页中span标签里面的t_id的值 public function getpreg(){ $www = 'http://monkey.test.tripb. ...

  8. 客户端登录验证 -- ESFramework 4.0 快速上手(15)

    在之前版本的Rapid引擎中,是没有提供客户端登陆验证的机制的,如果要验证用户的帐号密码信息,我们只有自己手动通过自定义信息来实现.在2011.04.25发布的新版本中,客户端Rapid引擎,则内置了 ...

  9. linux安装配置solr

    一.JDK的安装和配置 下载.解压jdk-7u79-linux-x64.gz 1.tar -zxvf jdk-7u79-linux-x64.gz -c /usr/java/ 解压到/usr/java/ ...

  10. AVFoundation下的视频分帧处理

    // // ViewController.m // VideoFrame // // Created by wiseman on 16/1/27. // Copyright (c) 2016年 wis ...