界面传值

四种传值的方式

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 单例 协议的更多相关文章

  1. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  2. iOS多页面传值方式之单例传值singleton

    // 要实现单例传值,那就必须得新建一个类做为单例 提供创建该类对象的类方法(因为是要在alloc开辟内存空间后赋值).所有在.h文件中声明该方法 + (instancetype)defaultUII ...

  3. spring学习 十七 scope属性,单例多例

    Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...

  4. 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,

    首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @ ...

  5. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  6. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

  7. iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...

  8. iOS传值之block传值(一)

    ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...

  9. SpringMVC中的controller默认是单例的原因

    http://lavasoft.blog.51cto.com/62575/1394669/ 1.性能 :单例不用每次new浪费资源时间. 2.不需要:一般controller中不会定义属性这样单例就不 ...

随机推荐

  1. mysql日志详细解析

    MySQL日志: 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志: 日志是mysql数据库的重要组成部分.日志文件中记录着mysql数据库运行期间发生的变化:也就是说用来记录mysql ...

  2. 将插入的新行放入dataGridView的第一行

    将插入的新行放入dataGridView的第一行 习惯这样用的: dataGridView1.Rows.Add(dataRow);改成:dataGridView1.Rows.Insert(0,data ...

  3. dom4j生成xml

    package com.yunfengtech.solution.business; import java.io.FileOutputStream; import org.dom4j.Documen ...

  4. Sql语句查看表结构

    快速查看表对的就说明,及表字段描述及字段类型 --表描述 SELECT tbs.name 表名,ds.value 描述 FROM sys.extended_properties ds LEFT JOI ...

  5. 使用php添加定时任务

    1.  php执行外部命令的函数:       system(),exec(),passthru()      注意点:     1.调用的路径,相对路径有时候不是很靠谱.           sys ...

  6. XE6移动开发环境搭建之IOS篇(6):设置Mac OSX的网络。(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 我们配置一下MAC的 ...

  7. [EventBus源码解析] EventBus.post 方法详述

    前情概要 上一篇blog我们了解了EventBus中register/unregister的过程,对EventBus如何实现观察者模式有了基本的认识.今天我们来看一下它是如何分发一个特定事件的,即po ...

  8. Karma: 3 - 测试覆盖率

    karma 的插件 karma-coverage 提供了测试代码覆盖率的支持. karma 的页面:http://karma-runner.github.io/0.8/config/coverage. ...

  9. webstrom 2016 破解

    本篇文章经过笔者实验结果,网友均可放心使用 版权声明:本文为博主原创文章,未经博主允许不得转载. 工欲善其事,必先利其器 websrtom作为前端开发神器,现在是越来越多的用户使用.很多小白们下载好了 ...

  10. [转载]: delphi中XLSReadWrite控件的使用(2)---delphi XE下安装

    一.下载 官方下载网址: http://www.axolot.com/components/download.htm 从这里可以下载到从Delphi5到DelphiXE全部支持的版本. 二.软件安装 ...