一、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. 3. 对系统表空间使用Raw磁盘分区

    3. 对系统表空间使用Raw磁盘分区 可以将raw磁盘分区用作InnoDB系统表空间中的数据文件.此技术可在Windows和某些Linux和Unix系统上启用非缓冲I/O,而无需文件系统开销.使用和不 ...

  2. C++中派生类使用基类成员的问题

    在C++中,派生类在定义构造函数时,会调用基类构造函数首先完成基类部分的构造: class Derive : public Base { public: Derive(string nam, int ...

  3. MySQL数据库安装与Navicat Premium 12 安装与破解

    一.文件下载: MySQL:官网:https://www.mysql.com/downloads/(现在最新的是5.7版) 下载路径:"Downloads" ==>> ...

  4. 【BZOJ 3289】 Mato的文件管理(离线算法莫队)

    Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能 ...

  5. 【转】windows下nginx+mono+fastCGI部署asp.net网站

    原文链接:http://www.cnblogs.com/amityat/archive/2011/08/23/2150153.html 1,什么是nginx 简介Nginx ("engine ...

  6. vsftpd系统用户配置详解

    1.安装yum -y install pam pam-devel db4 de4-devel db4-uitls db4-tclyum -y install vsftpd 新建vsftpd系统用户:u ...

  7. Linux 修改主机名

    1 vi /etc/sysconfig/network 2 vi /etc/hosts 3 hostname xxx 4 Done! 退出重连后生效

  8. 使用cURL和用户名和密码?

    问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...

  9. POJ 1159 字符串匹配问题

    题目大意: 问至少添加几个字符才能保证这个字符串是个回文串 一开始想也想不到字符串匹配上,因为是找回文串,我们可以把已给字符串逆向得到一个新的字符串,然后比较两者得到最大匹配长度,最后总长度减去最大匹 ...

  10. C 题 KMP中next[]问题

    题目大意: 找到能够进行字符串匹配的前缀 这题只要一直求next,直到next为0停止,记得答案是总长减去next的长度 #include <iostream> #include < ...