IOS 代码块传值
#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 代码块传值的更多相关文章
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...
- 一篇文章看懂iOS代码块Block
block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返 ...
- [转]iOS代码块Block
代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值,特殊地,Block还可以保存一段代码,在需要 ...
- iOS代码块block使用
代码块的本质是和其他的变量类似,不同的是,代码块存储的数据是一个函数体.使用代码块,你可以像调用其他标准函数一样的调用,可以传入参数,并得到返回值. 脱字符是代码块的语法标记.下图表示代码块的 ...
- IOS 代码块
1.关系式表示 <returnType>(^BlockName)(list of arguments)=^(arguments){body;};
- iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)
(一)属性传值 (二)代理(委托)传值 代理传值 适用于 反向传值 (从后往前传) 1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中 1.2 创建协议类型的 ...
- IOS学习4——block代码块
本文转载自:iOS开发-由浅至深学习block 一.关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想 ...
- 微信小程序富文本渲染组件html2wxml及html2wxml代码块格式化在ios下字体过大问题
1.组件使用: 之前微信小程序的富文本渲染组件用的wxParse,对普通富文本确实可以,但是对于代码格式pre标签则无法使用. 下面这个html2wxml很不错,可以支持代码高亮. 详细文档:http ...
- IOS开发之----代码块的使用(二)
iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...
随机推荐
- webpack +vue开发(3)
webpack的一些有用的命令 webpack --display-modules 在终端显示这些module,另外一个推荐使用 webpack --display-modules --display ...
- Spring定时器的时间表达式
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...
- 每天一个 Linux 命令(13):less 命令
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前面 ...
- linux下一对多socket服务器端多线程泄露问题
线程创建多了,没有释放.导致内存泄露... int main() { int len; int on=1; // pMachList = CreateEmptyLinklist(); DataBase ...
- python-appium识别元素等待时间
1.显式等待 一个显式等待是你定义的一段代码,用于等待某个条件发生然后再继续执行后续代码. from selenium import webdriverfrom selenium.webdriver. ...
- Java之重载与覆盖
有的时候,类的同一种功能有多种实现方式,到底采用哪种实现方式,取决于调用者给定的参数.例如我们最常用的System.out.println()能够打印出任何数据类型的数据,它有多种实现方式.运行时,J ...
- 'telnet' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
在telnet与ftp服务器相连时出现的问题: 原因:windows自带telnet,只是没有安装 解决如下: 然后关机重启
- python之fabric(一):环境env
原文:https://my.oschina.net/indestiny/blog/289587 1. fabric有很多可配置的环境,如: user:默认用于ssh登录的本地用户名. password ...
- Java和C++中的static
1.Java类中的static变量和static方法会在类装载的过程中就得到内存分配,然后就会进行初始化工作.最多可能会被初始化3次,静态代码块的执行在main方法之前. static变量不可以在构造 ...
- javascript generate a guid
function Guid() { var random = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); retu ...