#import <UIKit/UIKit.h>
typedef void (^MyBlock)(NSString*);
@interface SecondViewController : UIViewController @property (retain,nonatomic)UITextField* myTextField;
@property(copy,nonatomic)MyBlock block;
-(SecondViewController*)initWithBlock:(MyBlock)block; @end #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController
-(SecondViewController *)initWithBlock:(MyBlock)block
{
if (self=[super init]) {
self.block=block;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* btnTest=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnTest.frame=CGRectMake(, , , ) ;
[btnTest setTitle:@"Test" forState:UIControlStateNormal]; [btnTest addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTest]; UITextField* textField=[[UITextField alloc]initWithFrame:CGRectMake(, , , )];
textField.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
self.myTextField=textField;
}
-(void)back{
if (self.block) {
self.block(self.myTextField.text);
}
[self.navigationController popToRootViewControllerAnimated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end #import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIBarButtonItem* btnName=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(next)];
self.navigationItem.rightBarButtonItem=btnName;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)next{
SecondViewController* secondVC=[[SecondViewController alloc]initWithBlock:^(NSString* str){
NSLog(@"%@",str);
self.title=str;
}];
[self.navigationController pushViewController:secondVC animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

IOS 代码块传值的更多相关文章

  1. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值

    有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...

  2. 一篇文章看懂iOS代码块Block

    block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返 ...

  3. [转]iOS代码块Block

    代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值,特殊地,Block还可以保存一段代码,在需要 ...

  4. iOS代码块block使用

    代码块的本质是和其他的变量类似,不同的是,代码块存储的数据是一个函数体.使用代码块,你可以像调用其他标准函数一样的调用,可以传入参数,并得到返回值.     脱字符是代码块的语法标记.下图表示代码块的 ...

  5. IOS 代码块

    1.关系式表示 <returnType>(^BlockName)(list of arguments)=^(arguments){body;};

  6. iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)

    (一)属性传值 (二)代理(委托)传值 代理传值 适用于 反向传值 (从后往前传) 1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中 1.2 创建协议类型的 ...

  7. IOS学习4——block代码块

    本文转载自:iOS开发-由浅至深学习block 一.关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想 ...

  8. 微信小程序富文本渲染组件html2wxml及html2wxml代码块格式化在ios下字体过大问题

    1.组件使用: 之前微信小程序的富文本渲染组件用的wxParse,对普通富文本确实可以,但是对于代码格式pre标签则无法使用. 下面这个html2wxml很不错,可以支持代码高亮. 详细文档:http ...

  9. IOS开发之----代码块的使用(二)

    iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...

随机推荐

  1. SQL Server 2008 数据库镜像部署实例之一 数据库准备

    SQL Server 2008 数据库镜像部署实例之一 数据库准备 一.目标 利用Sql Server 2008 enterprise X64,建立异步(高性能)镜像数据库,同时建立见证服务器实现自动 ...

  2. Yii里增删改查的操作方法

    一.AR $model=New user();//user是数据库中的一张表,有id,name,pwd字段 1.增加: <1. $model->name='张三': $model-> ...

  3. php url编码解码

    urlencode 函数编码 urldecode 函数解码

  4. JVM内存结构之二--新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数

    一.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...

  5. 使用chrome联调不在同一个域的请求

    做前端的,用Ajax获取数据,是常有的事情,同域下自然没问题了,如果是不同域获取数据,浏览器就有个同源策略的限制. Origin * is not allowed by Access-Control- ...

  6. Blackfin DSP(六):BF533的SPORT接口

    1.特性 bf533有两个SPORT口(synchronous serial Port),即同步串行接口.完全独立的接收和发送通道,且每个通道都具有缓冲,最高速度可达SCLK/2.最大支持32bit字 ...

  7. IIS配置文件路径

    C:\Windows\System32\inetsrv\config\applicationHost.config

  8. MoleHill Getting Started AGAL(转)

    1.The OpCode This is what AGAL looks like: //vertex shader m44 op, va0, vc0 // pos to clipspace mov ...

  9. List集合转换为数组形式

    通过List集合对象的转类型函数 .ToArray() 如List<decimal> → deciaml[] 代码如下: var ComIdList = ComList.Select(p ...

  10. Java反射得到属性的值和设置属性的值(转)

    package com.whbs.bean; public class UserBean { private Integer id; private int age; private String n ...