#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. 使用C#和.NET的原因

    早在2000年6月,微软公布.NET之后不久,Ximian公司诞生了一个开源项目叫做Mono,运行在Linux环境下面的C#编译器和.NET Framework.十年后,在2011年,Ximian的创 ...

  2. Linq系列

    LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分

  3. httpwebrequest详解【转】

    http://blog.csdn.net/sjj2011/article/details/7823392 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最 ...

  4. fgets读取文件时的注意事项

    1 文本文件 a.txt 内容如下 2 c代码 FILE *fil; if (!(fil = fopen("/home/rudy/projects/paser/a.txt", &q ...

  5. mac下删除svn账号

    以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al drwxr-xr-x   6 linxyz  staff    204 ...

  6. 华为RH2285安装过程及经验总结

    安装测试 1.由于服务器为二手服务器,噪音相对较大,如果未经改造无法正常使用,当机器第一次运行的时候,我的血压一下升高不少. 第一步  服务器的远程端口和路由器连接在一起 第二步  设置服务器的bio ...

  7. JQ怎么获取margin-left的值

    var margin =$("#Modules .list").css('marginLeft');

  8. 2016-08-15:从YUV420P中提取指定大小区域

    typedef struct { int width; int height; }SizeInfo; typedef struct { int x; int y; int width; int hei ...

  9. PCI Express(三) - A story of packets, stack and network

    原文出处:http://www.fpga4fun.com/PCI-Express3.html Packetized transactions PCI express is a serial bus. ...

  10. 点击弹出固定大小的新窗口(js实现)

    <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html') --> </SCRIPT& ...