Blocks实现代理传值
一、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实现代理传值的更多相关文章
- View 与 Controller 之间的delegate(代理)传值
这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...
- iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)
(一)属性传值 (二)代理(委托)传值 代理传值 适用于 反向传值 (从后往前传) 1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中 1.2 创建协议类型的 ...
- iOS 页面间传值 之 属性传值,代理传值
手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...
- OS笔记047代理传值和block传值
在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...
- iOS 再谈 代理传值,block反向传值
本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...
- Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...
- 四大传值详解:属性传值,单例传值,代理传值,block传值
一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...
- 【iOS】代理传值与块代码传值
主线程与子线程常常须要进行数据的传递.不同的类之间,不同的控制器之间都须要. 并且常常须要监听一个动作的完毕.而后才去做对应事件. (代理是一对一的关系). 一.代理传值 代理是一种设计模式. iOS ...
随机推荐
- C#对象初始化的探讨
最近在弄MQ的性能监测数据埋点,无疑中用到一个Nstatsd的客户端,看到里面写过里面一种嵌套类的写法.代码如下: 客户端Client是一个密封的类,并且构造函数私有访问.然后又用一个嵌套类Curre ...
- 8. Truncate undo表空间
8. Truncate undo表空间 要Truncate Undo 表空间,必须为MySQL实例配置至少两个undo表空间(两个undo表空间可确保一个undo表空间保持活动状态,另一个处于脱机状态 ...
- docker-ce的安装
Docker提供了两个版本:社区版(CE)和企业版(EE). Docker社区版(CE)是开发人员和小型团队开始使用Docker并尝试使用基于容器的应用的理想之选.Docker CE有两个更新渠道,即 ...
- Linux磁盘管理及Lvm
1. 硬盘接口 IDE: SATA:常用: SCSI:主要用于高端服务器,linux默认: SAS 2. 硬盘种类 SATA硬盘:串口硬盘,有较强的纠错能力: SCSI 硬盘:默认硬盘: SAS 硬盘 ...
- 在docker中部署nginx
1端口映射 大写P 为容器暴漏的所有端口进行映射 -p ,--publish-all=true docker run -P -it centos /bin/bash 小写p 指定哪些容 ...
- 【struts2】学习笔记
常见问题及注意事项: 1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同! 2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突! 3. Registe ...
- 关于db访问层的封装设计感想 dbpy项目的开发
dbpy dbpy是一个python写的数据库CURD人性化api库.借鉴了 webpy db 和 drupal database 的设计. 如果喜欢 tornado db 或者 webpy db这类 ...
- xtu字符串 B. Power Strings
B. Power Strings Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java c ...
- python014 Python3 迭代器与生成器
Python3 迭代器与生成器迭代器迭代是Python最强大的功能之一,是访问集合元素的一种方式..迭代器是一个可以记住遍历的位置的对象.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结 ...
- Codeforces396A - On Number of Decompositions into Multipliers
Portal Description 给出\(n(n\leq500)\)个\([1,10^9]\)的数,令\(m=\prod_{i=1}^n a_i\).求有多少个有序排列\(\{a_n\}\),使得 ...