iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
实现了以下iOS页面间传值:
1.委托delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.单例模式方式;6.通过设置属性.
在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下:
情况1:A页面跳转到B页面
方法:
在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可
1.
//SecondViewController.h
1.
@property
(nonatomic) NSInteger flag;
//当前系统标示(0:其他传值方式;1:block传值方式)
在A页面的试图控制器中
1.
//RootViewController.m
1.
- (IBAction)showSecondView:(id)sender {
2.
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@
'SecondViewController'
bundle:nil];
3.
second.delegate = self;
4.
second.flag =
0
;
5.
[self presentViewController:second animated:YES completion:nil];
6.
}
情况2:A页面跳转到B页面,B页面再跳转回A页面
主流方案:
(1)通过委托delegate的方式实现
设置协议及方法
1.
1.
//SecondViewController.h
1.
@protocol
secondViewDelegate
2.
-(
void
)showName:(NSString *)nameString;
3.
@end
设置代理(为防止循环引用,此处采用了weak)
1.
//SecondViewController.h
1.
@interface
SecondViewController : UIViewController
2.
@property
(nonatomic, weak)id<secondViewDelegate> delegate;
3.
@property
(nonatomic, copy) ablock block;
4.
@end
调用
01.
//SecondViewController.m
02.
- (IBAction)delegateMethod:(id)sender {
03.
if
([self notEmpty]) {
04.
[self.delegate showName:self.nameTextField.text];
05.
[self dismissViewControllerAnimated:YES completion:nil];
06.
}
else
{
07.
[self showAlert];
08.
}
09.
}
显示
1.
//RootViewController.m
2.
-(
void
)showName:(NSString *)nameString{
3.
self.nameLabel.text = nameString;
4.
}
最重要也是最容易忽略的,就是一定要设置delegate的指向。
(2)通过通知notification的方式实现
- 在B页面的控制器中,发送通知:
01.
//SecondViewController.m
02.
- (IBAction)notificationMethod:(id)sender {
03.
if
([self notEmpty]) {
04.
[[NSNotificationCenter defaultCenter] postNotificationName:@
'ChangeNameNotification'
object:self userInfo:@{@
'name'
:self.nameTextField.text}];
05.
[self dismissViewControllerAnimated:YES completion:nil];
06.
}
else
{
07.
[self showAlert];
08.
}
09.
}
在A页面的控制器中,注册通知:
1.
//RootViewController.m
2.
- (
void
)viewDidLoad
3.
{
4.
[
super
viewDidLoad];
5.
// Do any additional setup after loading the view from its nib.
6.
[[NSNotificationCenter defaultCenter] addObserver:self selector:
@selector
(ChangeNameNotification:) name:@
'ChangeNameNotification'
object:nil];
7.
}
当我们不使用时,要记得删掉通知:
1.
//RootViewController.m
2.
-(
void
)dealloc{
3.
[[NSNotificationCenter defaultCenter] removeObserver:self];
4.
}
调用,显示
1.
//RootViewController.m
2.
3.
-(
void
)ChangeNameNotification:(NSNotification*)notification{
4.
NSDictionary *nameDictionary = [notification userInfo];
5.
self.nameLabel.text = [nameDictionary objectForKey:@
'name'
];
6.
}
(3)block方式实现
block介绍:http://blog.csdn.net/totogo2010/article/details/7839061
链接一篇描述block回调挺有意思的文章: http://blog.csdn.net/mobanchengshuang/article/details/11751671
分析:
在B试图控制器中,定义一个block,参数为字符串
1.
//SecondViewController.h
2.
typedef
void
(^ablock)(NSString *str);
1.
//SecondViewController.h
2.
3.
@property
(nonatomic, copy) ablock block;
在B试图控制器中,当输入名字,点击对应的确定按钮后
01.
- (IBAction)blockMethod:(id)sender {
02.
if
([self notEmpty]) {
03.
if
(self.block) {
04.
self.block(self.nameTextField.text);
05.
[self dismissViewControllerAnimated:YES completion:nil];
06.
}
07.
}
else
{
08.
[self showAlert];
09.
}
10.
}
在A试图显示,回调block
1.
- (IBAction)showSecondWithBlock:(id)sender {
2.
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@
'SecondViewController'
bundle:nil];
3.
[self presentViewController:second animated:YES completion:nil];
4.
second.block = ^(NSString *str){
5.
self.nameLabel.text = str;
6.
};
7.
}
在查阅资料的过程中,我还看到了以下几种方案:
(1)使用SharedApplication,定义一个变量来传递(感觉和单例的方式一样)
(2)使用文件,或者NSUserdefault来传递
01.
//通过文件或者UserDefault方式存值(感觉不太适合此类传值,如果要用文件或者UserDefault方式存值的话,可以考虑此方式)
02.
- (IBAction)userDefaultMethod:(id)sender {
03.
if
([self notEmpty]) {
04.
[[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:@
'myNameText'
];
05.
[self dismissViewControllerAnimated:YES completion:nil];
06.
}
else
{
07.
[self showAlert];
08.
}
09.
}
在A试图控制器显示
01.
-(
void
)viewDidAppear:(BOOL)animated{
02.
[
super
viewDidAppear:animated];
03.
//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可
04.
/*
05.
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'] length] != 0) {
06.
self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'];
07.
[[NSUserDefaults standardUserDefaults] setObject:@'' forKey:@'myNameText'];
08.
}
09.
DataSource *dataSource = [DataSource sharedDataSource];
10.
if ([dataSource.myName length] != 0) {
11.
self.nameLabel.text = dataSource.myName;
12.
dataSource.myName = @'';
13.
}
14.
*/
15.
}
(3)通过一个单例的class来传递
B试图控制器
01.
//通过单例方式传值(感觉不太适合此类传值,如果要用单例方式传值的话,可以考虑此方式)
02.
- (IBAction)singletonMethod:(id)sender {
03.
if
([self notEmpty]) {
04.
DataSource *dataSource = [DataSource sharedDataSource];
05.
dataSource.myName = self.nameTextField.text;
06.
[self dismissViewControllerAnimated:YES completion:nil];
07.
}
else
{
08.
[self showAlert];
09.
}
10.
}
A试图控制器显示
01.
-(
void
)viewDidAppear:(BOOL)animated{
02.
[
super
viewDidAppear:animated];
03.
//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可
04.
/*
05.
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'] length] != 0) {
06.
self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@'myNameText'];
07.
[[NSUserDefaults standardUserDefaults] setObject:@'' forKey:@'myNameText'];
08.
}
09.
DataSource *dataSource = [DataSource sharedDataSource];
10.
if ([dataSource.myName length] != 0) {
11.
self.nameLabel.text = dataSource.myName;
12.
dataSource.myName = @'';
13.
}
14.
*/
15.
}
16.
@end
这里面用到了单例模式,编写了DataSource这个类,存放数据
01.
//
02.
// DataSource.h
03.
// TestCallBack
04.
//
05.
// Created by csdc-iMac on 14-7-17.
06.
// Copyright (c) 2014年 JuneWang. All rights reserved.
07.
//
08.
09.
#
import
<Foundation/Foundation.h>
10.
11.
@interface
DataSource : NSObject
12.
@property
(nonatomic, strong) NSString *myName;
13.
+(DataSource*)sharedDataSource;
14.
@end
01.
//
02.
// DataSource.m
03.
// TestCallBack
04.
//
05.
// Created by csdc-iMac on 14-7-17.
06.
// Copyright (c) 2014年 JuneWang. All rights reserved.
07.
//
08.
09.
#
import
'DataSource.h'
10.
11.
@implementation
DataSource
12.
+(DataSource *)sharedDataSource{
13.
static
DataSource *dataSource = nil;
14.
static
dispatch_once_t once;
15.
dispatch_once(&once, ^{
16.
dataSource = [DataSource
new
];
17.
});
18.
return
dataSource;
19.
}
20.
@end
程序运行截图
A视图:
B视图
当输入姓名,并点击对应的确认按钮后,会回到A视图,并显示在B视图中输入的姓名
祝:玩得开心,有什么别的办法或者不正确的地方,欢迎指正。
如果写得不详细,可以通过源码分析。
参考:http://blog.csdn.net/cocoarannie/article/details/11857141
http://www.cnblogs.com/heri/archive/2013/03/18/2965815.html
源码地址:https://github.com/wangtao169447/PassValue
出处:http://www.cnblogs.com/JuneWang/p/3850859.html
延伸阅读:
iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)的更多相关文章
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- iOS页面间传值的六种方式
一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传 ...
- iOS 页面间传值 之 属性传值,代理传值
手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...
- iOS页面间传值的一些方式总结
废话不多说,直接进入主题: 这里要说的方式有6种:1.属性传值 2.block 3.delegate 4.UserDefault 5.单例 6.通知(篇幅原因我只写核心代码,如果看不懂可以直接在最下面 ...
- ios页面间跳转方式总结
转自:http://www.cnblogs.com/anywherego/p/3542202.html 下面以OldViewController(oldC)的按钮btn点击后跳转到NewViewCon ...
随机推荐
- es6小技巧
let myKey = 'variableKey'; let obj = { key1: 'One', key2: 'Two', [myKey]: 'Three' /* 棒呆! */ }; 给变量键加 ...
- .mtl文件格式解析
最近在导入下载的.OBJ文件,有时会出现只有模型而没有材质渲染的情况.难道材质要自己一点一点重新赋予?抓狂……我知道.OBJ文件用来存储模型信息,观察第一行代码,可以看到材质库文件为mtllib ## ...
- Android获取cpu和内存信息、网址的代码
android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...
- POJ 3978 Primes(求范围素数个数)
POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...
- ORACLE功能GREATEST功能说明具体实例
1 语法 GREATEST(expr_1, expr_2, ...expr_n) 2 说明 GREATEST(expr_1, expr_2, ...expr_n ...
- 在Qt中使用sleep
关于sleep函数,我们先来看一下他的作用:sleep函数是使调用sleep函数的线程休眠,线程主动放弃时间片.当经过指定的时间间隔后,再启动线程,继续执行代码.sleep函数并不能起到定时的作用 ...
- 关于 Java Collections API 您不知道的 5 件事--转
第 1 部分 http://www.ibm.com/developerworks/cn/java/j-5things2.html 对于很多 Java 开发人员来说,Java Collections A ...
- 前端的数据库:IndexedDB 。 ps:入门
应用程序需要数据.对大多数Web应用程序来说,数据在服务器端组织和管理,客户端通过网络请求获取.随着浏览器变得越来越有能力,因此可选择在浏览器存储和操纵应用程序数据. 本文向你介绍名为IndexedD ...
- Hibernate分页
1. HQL分页: Session session = HibernateUtil.getInstance().getSession(); Query query = session.createQu ...
- IOS 开发 【os x 使用常识】
开始看<learn Objective-C on the Mac>这本书,很基础,准备快速看完. 刚接触mac 的 os x 系统,很不适应,介绍一点我刚学的基本常识. 1.os x 显示 ...