iOS 使用Block进行逆传值
跟通知一样也是两个控制器,然后代码创建控件直接上代码
#import "ViewController.h"
#import "TwoViewController.h"
@interface ViewController ()
{
UIButton *_nextBtn;
UILabel *_showLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self show];
}
-(void)show{
_nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[_nextBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_nextBtn setTitle:@"下一个" forState:UIControlStateNormal];
[_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_nextBtn];
_showLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100,50)];
[_showLabel setBackgroundColor:[UIColor greenColor]];
[_showLabel setTextColor:[UIColor redColor]];
[self.view addSubview:_showLabel];
}
-(void)nextBtnClick:(UIButton *)nextBtnClick{
TwoViewController * two = [[TwoViewController alloc]init];
two.block = ^(NSString * str){
_showLabel.text = str;
};
[self presentViewController:two animated:YES completion:nil];
}
在TwoViewController.h中
#import <UIKit/UIKit.h>
typedef void (^MyBlock)(NSString *);
@interface TwoViewController : UIViewController
@property(nonatomic,copy)MyBlock block;
@end
在TwoViewController.m中
#import "TwoViewController.h"
@interface TwoViewController ()
{
UIButton *_backBtn;
UITextField *_textField;
}
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
[self shoulabel];
}
-(void)shoulabel{
_backBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
_backBtn.backgroundColor = [UIColor redColor];
[_backBtn setTitle:@"shang" forState:UIControlStateNormal];
[_backBtn addTarget:self action:@selector(shangBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_backBtn];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 250, 35)];
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview: _textField];
}
-(void)shangBtnClick:(UIButton *)shangBtn{
[self dismissViewControllerAnimated:YES completion:^{
if (self.block) {
self.block(_textField.text);
}
}];
}




这样就进行了简单的传值
iOS 使用Block进行逆传值的更多相关文章
- iOS Block界面反向传值
在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...
- iOS利用通知逆传值
直接创建两个控制器,点击跳转第二个界面,然后点击按钮进行传值 #import "ViewController.h" #import "TWOOViewController ...
- ios开发 block语句块
ios开发 block语句块 1.block 理解为匿名函数 2.block变量的定义 //定义block变量,^表示定义block //技巧:函数名左右加括号,在函数名前面在加^ void (^bl ...
- iOS中block的用法 以及和函数用法的区别
ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候 MyBlock(); 带参数的 ...
- iOS开发--Block
iOS开发--Block 1.什么是Block,block 的作用 ui开发和网络常见功能实现回调,按钮的事件处理方法是回调方法以及网络下载后的回调处理 (1)按钮 target-action 一 ...
- iOS之block
1. Block的声明和线程安全Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非ARC ...
- iOS开发——Block详解
iOS开发--Block详解 1. Block是什么 代码块 匿名函数 闭包--能够读取其他函数内部变量的函数 函数变量 实现基于指针和函数指针 实现回调的机制 Block是一个非常有特色的语法,它可 ...
- iOS中Block介绍(一)基础
ios开发block的使用指南,以及深入理解block的内存管理,也适用于osx开发.讨论范围:block的使用,内存管理,内部实现.不包含的内容:gc arc下的block内存,block在c++中 ...
- iOS中Block介绍 基础
ios开发block的使用指南,以及深入理解block的内存管理,也适用于osx开发.讨论范围:block的使用,内存管理,内部实现.不包含的内容:gc arc下的block内存,block在c++中 ...
随机推荐
- 搭建git for windows服务器(100%可以成功)
既然Git在Linux下面非常好用,为什么还要搭建git windows的服务器,因为不是所有的用户都需要在linux下面做开发,对吧,还有很多用户选择使用windows做开发. 看到很多网友尝试部分 ...
- Python之路----------ConfigParser模块
Python的ConfigParser 废话不说,拿去用 #coding=utf-8 import configparser ''' 基於Python3.0版本寫的配置文件的創建.增加.刪除.修改等方 ...
- SQLite如何测试
原文 http://www.sqlite.org/testing.html 目录 1. 介绍 1.1. 执行总结 2. 测试套件 3.异常测试 3.1 内存溢出测试 3.2 I/O错误测试 3.3 c ...
- GPS部标平台的架构设计(二) 可扩展性设计
在设计的前夕,设计人员喜欢把领导对未来业务的期望带入到设计目标当中,比如当前业务也不过是接入几千辆车,未来业务增长也不过几万台,但领导很多激情,强势要求二期平台的接入能力要达到20万台,这个要求带入到 ...
- with CTE AS
CTE/表变量/Temp http://www.cnblogs.com/ziyeyimeng/articles/2366855.html
- win10 pro 1511 激活成功
slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX slmgr /skms franklv.ddns.net slmgr /ato
- DataList删除操作
<asp:DataList ID="fileList" runat="server" RepeatColumns="1" Repeat ...
- rdesktop共享剪贴板的问题
使用-r clipboard:PRIMARYCLIPBOARD参数来共享剪贴板,连接到window7 但有时就不好用了,剪贴板没有同步 找到一些相关的资料 https://bugs.launchpad ...
- 在Excel里用vba给合并的单元格添加注释
Excel里使用VBA对已经合并的单元格添加注释,直接使用AddComment会报: 运行时错误 '1004':应用程序定义或者对象定义错误 找了很多文章都没找到怎么解决,最后发现在AddCommen ...
- EntityFramework 使用经验
1.Nuget控制台常用命令 1.获取EntityFramework命令帮助:get-help EntityFramework 2.在项目中启动数据迁移:Enable-Migrations 3.添加 ...