传值 属性 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中不会定义属性这样单例就不 ...
随机推荐
- starUML 建模
1. starUML 序列图建模 参考: [1]. starUML 序列图建模. http://blog.csdn.net/u013474104/article/details/43818691
- VM 虚拟机 Error 1324. The path My Documents contains a invalid chara 。
当安装VM(虚拟机)时,安装到一半时,提示:Error 1324. The path My Documents contains a invalid chara . 就是提示路径无效. 按下面的路径: ...
- Android开发--布局
一:LinearLayout 1.线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上.每一个LinearLayout里面又可分为垂直布局(android:orie ...
- string.Format 格式化输出日期
string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03) string.Format("{0 ...
- findstr 命令
body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...
- FireDAC 超时
FireDAC 超时 Timeout expired 在Win10 正常. 在Win7 CB的DLL 正常,Delphi的DLL怎么会超时呢??? 果然是连接字符串错了.改为正确的就连接正常了!
- IOS App Integrate Google Map Problems and Method to solve them
1. You must get a key in google developer center, and register it in below function in AppDelegate.m ...
- XidianOJ 1182 Chinese Paladin – Qi’s troubles
题目描述 As we all know, Xiahou Jinxuan (Chinese Paladin 5 Prequel's protagonist) and Yue Jinzhao (Chine ...
- Kafka深入理解-2:Kafka的Log存储解析
摘自http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互 ...
- {POJ}{动态规划}{题目列表}
动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...