传值 属性 block 单例 协议
界面传值
四种传值的方式
1、属性传值(从前往后)
步骤:
1、属性传值用于第一个界面向第二个界面传值
2、明确二者联系的桥梁,也就是触发跳转的地方
3、明确传输的值,类型是什么
4、在第二个视图控制器内部声明相对应类型的属性,来接受传输的值
5、在第二个界面使用传入的值
2、代理传值(从后往前)
步骤:
1、声明协议
UI中的协议名称 为 当前类名 + Delegate
@protocol FourViewControllerDelegate<NSObject>
- (void)pushValue:(NSString *)text;
- (void)pushValue1:(UIColor *)color;
@end
2、声明代理
@property (nonatomic, assign) id<FourViewControllerDelegate>delegate;
3、让代理执行方法(在页面跳转处)
//判断代理人不为空,并且代理人响应了协议中的方法
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue:)]) {
[self.delegate pushValue:self.textField.text];
[self.delegate pushValue1:self.view.backgroundColor];
}
4、指定代理人为自身
- (void)button {
FourViewController *four = [[FourViewController alloc] init];
//4、指定代理对象为自己
four.delegate = self;
[self.navigationController pushViewController:four animated:YES];
}
5、接受协议
@interface ThirdViewController : UIViewController<FourViewControllerDelegate>
6、实现协议方法
- (void)pushValue:(NSString *)text {
_lable.text = text;
}
- (void)pushValue1:(UIColor *)color{
self.view.backgroundColor = color;
}
3、block传值(从后往前)
有两种方式:
一、使用block属性实现回调传值
步骤:
1、定义需要传的值得类型
typedef void (^Bada)(NSString *);
typedef void (^Color) (UIColor *);
2、将Bloak声明成属性必须用copy修饰
@property (nonatomic, copy) Bada bada;
@property (nonatomic, copy) Color color;
3、
- (void)button {
//执行Block
if (self.bada != nil) {
self.bada(self.textField.text);
}
if (self.color != nil) {
self.color (self.view.backgroundColor);
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
4、对block进行实现
SecondViewController *second = [[SecondViewController alloc] init];
second.bada = ^(NSString *str) {
self.lable.text = str;
};
second.color = ^(UIColor *color) {
self.view.backgroundColor = color;
};
[self.navigationController pushViewController:second animated:YES];
方式二:在方法中定义block实现回调传值
步骤:
1、在First.h文件中,重定义void(^)(NSString *string)类型 的别名为FirstBlock
typedef void(^FirstBlock)(NSString *string);
2、声明方法,在方法中封装block
-(void)sendNumber:(NSInteger )number andBlock: (FirstBlock)block;
3、在First.m实现 法,并执 block
-(void)sendNumber:(NSInteger )number andBlock: (FirstlBlock)block;
{
NSString *string = [NSString
stringWithFormat:@"%ld",number];
block(string);
}
4、 执行放法,实现block并接收回传过来的string值
First*first= [First alloc] init];
//执行方法
[first sendNumber:12243432 andBlock:^(NSString
*string) {
self.label.text = string;
}];
4、单例传值(可用于从第一页面直接传值给第十个页面,既可以从前往后,也可以从后往前)
步骤:
1、创建一个类继承自NSObject,在.h文件中声明一个类方法。
//单利方法是类方法, 返回值类型为instancetype
//自己定义的单例类,方法名通常都是以share开头
+ (instancetype)shareInstance;
2、在.m文件中写类方法的实现
static Handler *handler = nil;
@implementation Handler
//怎么保证 创建的 当刘对象是唯一的?
+ (instancetype)shareInstance {
@synchronized(self) {//加锁,保证创建过程同一时间内只允许一个线程对象访问,保证其唯一性(self 任意对象)
if (handler == nil) {
handler = [[Handler alloc] init];
}
}
return handler;
}
3、借助视图的即将出现和即将消失的方法,可以实现值得传递。(注意视图的加载和创建方法只会走一次,但是视图的将出现和即将消失的方法会走多次)
- (void)viewWillDisappear:(BOOL)animated {
//创建单例
Handler *handler = [Handler shareInstance];
handler.string= self.textField.text;
}
- (void)viewWillAppear:(BOOL)animated {
Handler *handler = [Handler shareInstance];
self.textField.text = handler.string;
}
传值 属性 block 单例 协议的更多相关文章
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- iOS多页面传值方式之单例传值singleton
// 要实现单例传值,那就必须得新建一个类做为单例 提供创建该类对象的类方法(因为是要在alloc开辟内存空间后赋值).所有在.h文件中声明该方法 + (instancetype)defaultUII ...
- spring学习 十七 scope属性,单例多例
Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @ ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- iOS传值之block传值(一)
ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...
- SpringMVC中的controller默认是单例的原因
http://lavasoft.blog.51cto.com/62575/1394669/ 1.性能 :单例不用每次new浪费资源时间. 2.不需要:一般controller中不会定义属性这样单例就不 ...
随机推荐
- 删除空文件夹 清除CS扩展名文件 bat
删除空文件夹.删的干净.删的彻底. 将下列代码复制到txt中保存.并把后缀.txt命成.bat.然后运行即可. 方案1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- jmeter的逻辑控制器
这篇是在网上找的,写的实在是比我写的具体得多,也没什么好补充的,拿来记录一下,方便以后查询,感激原作者!! JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行 ...
- jexus
Jexus web server V5.1 安装配置要点 一.Jexus简介:Jexus web server for linux 是一款基于.NET兼容环境,运行于Linux/unix操作系统之上, ...
- 5.Makefile的原理及应用
1.概念 目标:目标顶格写,后面是冒号(冒号后面是依赖) 依赖:依赖是用来产生目标的原材料. 命令:命令前面一定是两个Tab,不能是定格,也不能说多个空格.命令就是要生成那个目标需要做的动作. 2.基 ...
- apache-jmeter学习文档
http://www.cnblogs.com/TankXiao/p/4045439.html#sampler
- Gradient Boosting Decision Tree学习
Gradient Boosting Decision Tree,即梯度提升树,简称GBDT,也叫GBRT(Gradient Boosting Regression Tree),也称为Multiple ...
- bootstrap分页插件--Bootstrap Paginator的使用&AJAX版备份(可单独使用)
html部分: <ul class="pagination"></ul> <!--bootstrap3版本用ul包裹--> <div cl ...
- 关于web测试
关于web测试1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是 ...
- [转]linux /proc/cpuinfo 文件分析
在Linux系统中,提供了proc文件系统显示系统的软硬件信息.如果想了解系统中CPU的提供商和相关配置信息,则可以通过/proc/cpuinfo文件得到.本文章针对该文件进行简单的总结. 基于不同指 ...
- 星号代替数字 js语句
在做登陆界面时,有这样一个需求,就是输入密码时,以密文形式展示(*),由于html5的属性 type="password" 只能以圆点形式展示, 下面方法能以星号代替输入符合 d ...