iOS开发——代理与block传值
一、代理传值的方法
1.Hehe1ViewController.h中
#import <UIKit/UIKit.h>
@protocol Hehe1ViewControllerDelegate <NSObject>
- (void)backValueWith:(NSString*)str;
@end
@interface Hehe1ViewController : UIViewController
@property(nonatomic,weak) id delegate;
@end
2.Hehe1ViewController.m中
#import "Hehe1ViewController.h"
@interface Hehe1ViewController ()
@property(nonatomic,strong) UITextField *heheTextField;
@end
@implementation Hehe1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
self.heheTextField = heheTextField;
heheTextField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:heheTextField];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(20, 250, 300, 50);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"Back to VC" forState:UIControlStateNormal];
[button1 setTintColor:[UIColor whiteColor]];
[button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
- (void)goToHehe1 {
NSLog(@"hehe1...");
[_delegate backValueWith:self.heheTextField.text];
[self.navigationController popViewControllerAnimated:YES];
}
@end
二、block传值
1.Hehe2ViewController.h中
#import <UIKit/UIKit.h>
typedef void(^BackValue)(NSString *str);
@interface Hehe2ViewController : UIViewController
@property(nonatomic,copy) BackValue backValue;
- (void)returnStr:(BackValue)block;
@end
2.Hehe2ViewController.m中
#import "Hehe2ViewController.h"
@interface Hehe2ViewController ()
@property(nonatomic,strong) UITextField *heheTextField;
@end
@implementation Hehe2ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
self.heheTextField = heheTextField;
heheTextField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:heheTextField];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(20, 250, 300, 50);
button2.backgroundColor = [UIColor orangeColor];
[button2 setTitle:@"Back to VC" forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
[button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)returnStr:(BackValue)block {
self.backValue = block;
}
- (void)goToHehe2 {
NSLog(@"hehe2...");
self.backValue(self.heheTextField.text);
[self.navigationController popViewControllerAnimated:YES];
}
@end
三、在ViewController.m中接收传回的值
#import "ViewController.h"
#import "Hehe1ViewController.h" //delegate
#import "Hehe2ViewController.h" //block
#define klSceenWidth self.view.bounds.size.width
@interface ViewController ()
@property(nonatomic,strong) UILabel *label1;
@property(nonatomic,strong) UILabel *label2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, klSceenWidth-40, 30)];
self.label1 = label1;
[self.view addSubview:label1];
//delegate
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(20, 150, klSceenWidth-40, 30);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"go to hehe1" forState:UIControlStateNormal];
[button1 setTintColor:[UIColor whiteColor]];
[button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
//block
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, klSceenWidth-40, 30)];
self.label2 = label2;
[self.view addSubview:label2];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(20, 250, klSceenWidth-40, 30);
button2.backgroundColor = [UIColor orangeColor];
[button2 setTitle:@"go to hehe1" forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
[button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)backValueWith:(NSString*)str {
self.label1.text = str;
}
- (void)goToHehe1 {
Hehe1ViewController *heheVC1= [[Hehe1ViewController alloc] init];
heheVC1.delegate = self;
[self.navigationController pushViewController:heheVC1 animated:YES];
}
- (void)goToHehe2 {
Hehe2ViewController *heheVC2= [[Hehe2ViewController alloc] init];
[heheVC2 returnStr:^(NSString *str) {
self.label2.text = str;
}];
[self.navigationController pushViewController:heheVC2 animated:YES];
}
@end
iOS开发——代理与block传值的更多相关文章
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...
- iOS开发-Objective-C Block的实现方式
前言:我们可以把Block当作一个闭包函数,它可以访问外部变量和局部变量,但默认是不可以修改外部变量.你可以使用它来做回调方法,比起使用代理(Delegate)会更加直观.顺带一提,苹果很多的接口(A ...
- iOS开发-代理模式
代理模式有的时候也被称之为委托模式,但是实际上两者是有分别的,代理模式为另一个对象提供一个替身或占位符访问这个对象,代理对象和控制访问对象属于同一类,委托对象和对象不一定属于同一类.两者都可以控制类的 ...
- swift-delegate(代理)或者block传值
1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...
- iOS开发之使用block块进行数据遍历的方法
看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...
- iOS开发 总结几种传值--extern,NSUserDefaults,Delegate
1 设置委托(代理模式) 建一个委托testViewDelegate.h #import//b中的参数传到a//设置委托方法,例如本文件//在b中.h描述NSObject * deleg ...
- iOS开发——语法&高级Block练习
高级Block练习 一 .最简单的block使用 使用block的三个步骤:1.定义block变量 2.创建block代码块 3.调用block匿名函数 定义一个block的构成包括:返回值,bloc ...
- iOS开发-多层嵌套block中如何使用__weak和__strong
1.关于__weak__weak只能在ARC模式下使用,也只能修饰对象(比如NSString等),不能修饰基本数据类型(比如int等)__weak修饰的对象在block中不可以被重新赋值.__weak ...
- iOS开发-委托(Delegate)浅谈
委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多.委托是Cocoa中最简单.最灵活 ...
随机推荐
- 编译Android各种错误
第一次编译成功,第二次出现Value for 'keystore' is not valid. It must resolve to a single path 打开proj.android\ant. ...
- git bash退回上一个文件夹
cd ..\ a@w3311 MINGW32 /f/Projects/crm (master) $ cd..\ > bash: cd..: command not found a@w3311 M ...
- Itext简绍及操作PDF文件
iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- Windows下python安装MySQLdb
安装MySQLdb需要在电脑上安装MySQL connector C,只需要这个connector就好,不需要把mysql装全. 另外,需要安装VC for python提供编译. 到官网上下载脚本进 ...
- jq的事件冒泡
在页面上可以有多个事件,也可以多个元素响应同一件事, 事件冒泡引发的问题: 有些时候不想动用的事件,却因为事件冒泡而触发 解决问题: 1.事件对象 由于IE-DOM和标准的DOM实现事件对象的方法各不 ...
- Nginx反向代理导致PHP获取不到正确的HTTP_HOST,SERVER_NAME,客户端IP的解决方法
今天第一次陪Nginx负载均衡,发现PHP无法获取HTTP_HOST 贴上的Nginx配置 upstream abc.com { server 10.141.8.55:8005; server 10. ...
- C++设计模式-单件
理解 1. Singleton模式用来取代全局静态变量.C++通过静态成员变量来实现类实例全局唯一性. 2. instance()方法是单件提供的唯一调用入口. 要点 1. ...
- mysql慢查询问题
[问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时候,查询的记录数才几万条,但查询的速度非常慢,大概要4~5分钟左右 [处理过程] 1)explain 首先怀疑索引没 ...
- tabBar自定义
有时系统的tabBar并不能满足我们的开发需求: 这时,我们需要自定义一个tabBar.直接上代码: // 在tabBarController中用KVC更换掉系统tabBar [self setVal ...
- div使用
div style常用属性 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height ...