传值 属性 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中不会定义属性这样单例就不 ...
随机推荐
- Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持
.net framework 4.5下测试成功,使用RedisSessionStateProvider 2.2.1保持session数据,通过Haproxy保持会话数据.首先在PM下安装RedisSe ...
- repeat语句
一.repeat语句格式repeat语句用于"重复执行循环体,直到指定的条件为真时为止" repeat语句格式:repeat 语句1; 语句2; -- 语句n;until ...
- ORA-00245: control file backup failed; target is likely on a local file system
ORACLE11G RAC alert报错如下:Errors in file /u01/app/oracle/diag/rdbms/dljyzs/dljyzs1/trace/dljyzs1_ora_8 ...
- Raising Modulo Numbers
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- ZYB's Premutation POJ5592
Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...
- 关于SoCFPGA 编译问答
1.在Qsys里面添加了一个新的组件(不是hps里面的东西),挂在Avalon总线上,如添加了一个新ADC组件,是否需要重新编译dtb. 答: 不需要. 2.修改了相关Qsys里面的东西,是否也需要 ...
- Range-Based for Loops
for ( decl : coll ) { statement } where decl is the declaration of each element of the passed collec ...
- cocos2d中的可见性检测
游戏的在进行一次渲染的时候,通常会提交大量的渲染对象给gpu.在这些需要渲染的对象中,并不是所有对象都会出现镜头中,即有一部分对象是不可见的. 通常有两种方式来完成不可见对象的剔除工作: (1)直接交 ...
- 通过AngularJS实现图片上传及缩略图展示
从项目中截出的代码 HTML部分: <section> <img src="image/user-tuijian/tuijian_banner.png" /> ...
- 如何为IIS增加svg和woff格式文件的支持
字体文件来显示矢量的图标,为了能在IIS上正常显示图标,可以通过增加iis的MIME-TYPE来支持图标字体文件: 增加以下两种文件类型即可: .woff application/x-woff.svg ...