一、RootViewController:

#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
{
UILabel *_myLabel; }
@end @implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"第一页";
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
self.navigationItem.rightBarButtonItem = item; _myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 50)];
_myLabel.textAlignment = NSTextAlignmentCenter;
_myLabel.text = @"Blocks";
[self.view addSubview:_myLabel];
// Do any additional setup after loading the view from its nib.
}
-(void)nextPage{
SecondViewController *second = [[SecondViewController alloc]initWithBlock:^(NSString *str) {
NSLog(@"%@",str);
_myLabel.text = str;
}];
[self.navigationController pushViewController:second animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

二、SecondViewConroller:

.h文件

#import <UIKit/UIKit.h>
typedef void(^myBlock)(NSString *); @interface SecondViewController : UIViewController
{
myBlock block;
}
-(id)initWithBlock:(myBlock)str;
@end

.m文件

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
-(id)initWithBlock:(myBlock)str{
self = [super init];
if(self)
{
block = str;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 100, 50);
[myButton setTitle:@"点我传值!" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton]; // Do any additional setup after loading the view from its nib.
}
-(void)clicked{
NSLog(@"我被点击了! "); if (block) {
block(@"哈哈");
}
//[self.navigationController popViewControllerAnimated:YES];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

Blocks实现代理传值的更多相关文章

  1. View 与 Controller 之间的delegate(代理)传值

    这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...

  2. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值

    有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...

  3. iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)

    (一)属性传值 (二)代理(委托)传值 代理传值 适用于 反向传值 (从后往前传) 1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中 1.2 创建协议类型的 ...

  4. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  5. OS笔记047代理传值和block传值

    在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...

  6. iOS 再谈 代理传值,block反向传值

    本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...

  7. Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值

    一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...

  8. 四大传值详解:属性传值,单例传值,代理传值,block传值

    一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...

  9. 【iOS】代理传值与块代码传值

    主线程与子线程常常须要进行数据的传递.不同的类之间,不同的控制器之间都须要. 并且常常须要监听一个动作的完毕.而后才去做对应事件. (代理是一对一的关系). 一.代理传值 代理是一种设计模式. iOS ...

随机推荐

  1. Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)

    1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...

  2. mybatis动态sql foreach的用法

    <select id="findUserByIds" parameterType="com.pojo.QueryVo" resultType=" ...

  3. 后缀排序(codevs 1500)

    题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个长度为n的由小写字母构成的字符串,要求他把该字符串的n个后缀(suffix)从小到大排序. 何谓后缀?假设 ...

  4. POJ3233:Matrix Power Series

    对n<=30(其实可以100)大小的矩阵A求A^1+A^2+……+A^K,K<=1e9,A中的数%m. 从K的二进制位入手.K分解二进制,比如10110,令F[i]=A^1+A^2+……+ ...

  5. hdu4115:Eliminate the Conflict

    n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...

  6. 深入理解计算机操作系统——第11章:全球IP英特网

    全球IP英特网 (1)每台英特网主机都运行实现TCPIP协议的软件. (2)英特网的客户端和服务器混合使用套接字接口函数和Unix IO函数来进行通信. (3)套接字函数典型的是作为陷入内核的系统调用 ...

  7. noj 2033 一页书的书 [ dp + 组合数 ]

    传送门 一页书的书 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 53            测试通过 : 1 ...

  8. poj1190,DFS/已知一个等式,求另一个最小值

    7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.  设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱. ...

  9. Swift--错误集:couldn’t be opened because you don’t have permission to view it

    bug复现过程  把snapkit拉入代码中时,也把里面的info.plist文件拖到项目中,运行时,提示“couldn’t be opened because you don’t have perm ...

  10. T1992 聚会 codevs

    http://codevs.cn/problem/1992/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小S 想要从某地 ...