传值 属性 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中不会定义属性这样单例就不 ...
随机推荐
- zen coding和emmet
zen coding 改名为 emmet http://emmet.io/download/
- GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)
[注]原文 http://www.cnblogs.com/beniao/archive/2010/04/18/1714544.html 墨卡托投影(Mercator Projection),又名&qu ...
- C# string类型和byte[]类型相互转换
string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: ...
- Matrix的一些知识
1.什么是ColorMatrix ColorMatrix是一个颜色矩阵,它定义了一个 4*5 的float[]类型的矩阵 颜色矩阵,而图像的 RGBA 值则存储在一个5*1的颜色分量矩阵C中 所以为了 ...
- jQuery triger与trigerHandler的区别
trigger(event, [data]) 与 triggerHandler(event, [data]) 都是用于触发一个事件. 其两者的区别在于,如果触发的事件是有浏览器默认行为的,trigge ...
- Jquery Datatables 请求参数及接收参数处理
Jquery Datatables 请求参数及接收参数处理 /** * Created by wb-wuyifu on 2016/8/9. */ /** * Created by wb-wuyifu ...
- php 连接mongodb 增查改删操作
查询 <?php $m=new MongoClient('mongodb://admin:admin@localhost:27017/admin'); $db=$m->hndb; $cc= ...
- java并发编程_CountDownLanch(倒计数锁存器)应用场景
使用介绍: 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在 ...
- Bootstrap导航点击菜单跳转与点击缩放菜单折叠按钮缓冲效果插件jquery.singlePageNav.min.js
引入步骤: <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></scrip ...
- CSS-页面布局
介绍 几个实现多栏布局的方法.主要介绍使用内部div来创建浮动的栏. 多栏布局有三种基本的实现方案:固定宽度.流动.弹性. 固定宽度布局的大小是随用户调整浏览器窗口大小而变化,一般是900至1100像 ...