一、代理传值的方法

  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传值的更多相关文章

  1. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

  2. iOS开发-Objective-C Block的实现方式

    前言:我们可以把Block当作一个闭包函数,它可以访问外部变量和局部变量,但默认是不可以修改外部变量.你可以使用它来做回调方法,比起使用代理(Delegate)会更加直观.顺带一提,苹果很多的接口(A ...

  3. iOS开发-代理模式

    代理模式有的时候也被称之为委托模式,但是实际上两者是有分别的,代理模式为另一个对象提供一个替身或占位符访问这个对象,代理对象和控制访问对象属于同一类,委托对象和对象不一定属于同一类.两者都可以控制类的 ...

  4. swift-delegate(代理)或者block传值

    1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...

  5. iOS开发之使用block块进行数据遍历的方法

    看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...

  6. iOS开发 总结几种传值--extern,NSUserDefaults,Delegate

    1 设置委托(代理模式)      建一个委托testViewDelegate.h   #import//b中的参数传到a//设置委托方法,例如本文件//在b中.h描述NSObject * deleg ...

  7. iOS开发——语法&高级Block练习

    高级Block练习 一 .最简单的block使用 使用block的三个步骤:1.定义block变量 2.创建block代码块 3.调用block匿名函数 定义一个block的构成包括:返回值,bloc ...

  8. iOS开发-多层嵌套block中如何使用__weak和__strong

    1.关于__weak__weak只能在ARC模式下使用,也只能修饰对象(比如NSString等),不能修饰基本数据类型(比如int等)__weak修饰的对象在block中不可以被重新赋值.__weak ...

  9. iOS开发-委托(Delegate)浅谈

    委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多.委托是Cocoa中最简单.最灵活 ...

随机推荐

  1. 最短路<dijk>

    题意: 有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路.按格式输出. power oj 2443 题解: 标准dijk算法. #include<c ...

  2. 第一个python实例程序

    #!/usr/bin/python2.7 import os ls = os.linesep fname = raw_input("fname:"); while True: if ...

  3. C++文件编程(文件流操作)

    给出了比较常见的文件操作,包括二进制文件操作.代码如下: #include<iostream> #include<cstdio> #include<cstring> ...

  4. POJ 1236 Network of Schools(tarjan算法 + LCA)

    这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...

  5. elasticsearch 索引优化

    ES索引优化篇主要从两个方面解决问题,一是索引数据过程:二是检索过程.  索引数据过程我在上面几篇文章中有提到怎么创建索引和导入数据,但是大家可能会遇到索引数据比较慢的过程.其实明白索引的原理就可以有 ...

  6. Django - 模型表单(创建、更新、删除)

    urls.py # /music/alubm/add/ url(r'^album/add/$', views.AlbumCreate.as_view(), name="album-add&q ...

  7. 随机法解决TSP问题

    TSP问题一直是个头疼的问题,但是解决的方法数不胜数,很多的算法也都能解决.百度资料一大堆,但是我找到了代码比较简练的一种.随机法.下面只是个人的看法而已,如果有任何问题虚心接受. 顾名思义,随机法就 ...

  8. hdu_4352_XHXJ's LIS(数位DP+状态压缩)

    题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...

  9. JSP标准标签库(JSTL)--SQL标签库 sql

    了解即可.SQL标签库 No. 功能分类 标签名称 描述 1 数据源标签 <sql:setDataSource> 设置要使用的数据源名称 2 数据库操作标签 <sql:query&g ...

  10. Linux下find命令用法小结

    find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...