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

实现了以下iOS页面间传值:1.委托delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.单例模式方式;6.通过设置属性,实现页面间传值

在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下:

情况1:A页面跳转到B页面

方法:

在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可

//SecondViewController.h
@property(nonatomic) NSInteger flag;//当前系统标示(0:其他传值方式;1:block传值方式)

在A页面的试图控制器中

//RootViewController.m
- (IBAction)showSecondView:(id)sender {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
second.delegate = self;
second.flag = 0;
[self presentViewController:second animated:YES completion:nil];
}

情况2:A页面跳转到B页面,B页面再跳转回A页面

主流方案:

(1)通过委托delegate的方式实现

设置协议及方法

 

//SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, weak)id<secondViewDelegate> delegate;
@property (nonatomic, copy) ablock block;
@end
 
 调用
 

显示

 
//RootViewController.m
-(void)showName:(NSString *)nameString{
self.nameLabel.text = nameString;
}
最重要也是最容易忽略的,就是一定要设置delegate的指向。
 
 

(2)通过通知notification的方式实现

在B页面的控制器中,发送通知:
//SecondViewController.m
- (IBAction)notificationMethod:(id)sender {
if ([self notEmpty]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"name":self.nameTextField.text}];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self showAlert];
}
}

在A页面的控制器中,注册通知:

//RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
}

当我们不使用时,要记得删掉通知:

//RootViewController.m
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

调用,显示

//RootViewController.m

-(void)ChangeNameNotification:(NSNotification*)notification{
NSDictionary *nameDictionary = [notification userInfo];
self.nameLabel.text = [nameDictionary objectForKey:@"name"];
}

(3)block方式实现

block介绍:http://blog.csdn.net/totogo2010/article/details/7839061

链接一篇描述block回调挺有意思的文章:http://blog.csdn.net/mobanchengshuang/article/details/11751671

分析:

在B试图控制器中,定义一个block,参数为字符串

//SecondViewController.h
typedef void (^ablock)(NSString *str);
//SecondViewController.h

@property (nonatomic, copy) ablock block;

在B试图控制器中,当输入名字,点击对应的确定按钮后

- (IBAction)blockMethod:(id)sender {
if ([self notEmpty]) {
if (self.block) {
self.block(self.nameTextField.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
}else{
[self showAlert];
}
}

在A试图显示,回调block

- (IBAction)showSecondWithBlock:(id)sender {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
second.block = ^(NSString *str){
self.nameLabel.text = str;
};
}

在查阅资料的过程中,我还看到了以下几种方案:

(1)使用SharedApplication,定义一个变量来传递(感觉和单例的方式一样)

(2)使用文件,或者NSUserdefault来传递

//通过文件或者UserDefault方式存值(感觉不太适合此类传值,如果要用文件或者UserDefault方式存值的话,可以考虑此方式)
- (IBAction)userDefaultMethod:(id)sender {
if ([self notEmpty]) {
[[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:@"myNameText"];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self showAlert];
}
}

在A试图控制器显示

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可
/*
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {
self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];
}
DataSource *dataSource = [DataSource sharedDataSource];
if ([dataSource.myName length] != 0) {
self.nameLabel.text = dataSource.myName;
dataSource.myName = @"";
}
*/
}

(3)通过一个单例的class来传递

B试图控制器

//通过单例方式传值(感觉不太适合此类传值,如果要用单例方式传值的话,可以考虑此方式)
- (IBAction)singletonMethod:(id)sender {
if ([self notEmpty]) {
DataSource *dataSource = [DataSource sharedDataSource];
dataSource.myName = self.nameTextField.text;
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self showAlert];
}
}

A试图控制器显示

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//如果想测试通过UserDefault方式传值或者通过单例方式传值,取消以下注释即可
/*
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {
self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];
}
DataSource *dataSource = [DataSource sharedDataSource];
if ([dataSource.myName length] != 0) {
self.nameLabel.text = dataSource.myName;
dataSource.myName = @"";
}
*/
}
@end

这里面用到了单例模式,编写了DataSource这个类,存放数据

//
// DataSource.h
// TestCallBack
//
// Created by csdc-iMac on 14-7-17.
// Copyright (c) 2014年 JuneWang. All rights reserved.
// #import <Foundation/Foundation.h> @interface DataSource : NSObject
@property (nonatomic, strong) NSString *myName;
+(DataSource*)sharedDataSource;
@end 
//
// DataSource.m
// TestCallBack
//
// Created by csdc-iMac on 14-7-17.
// Copyright (c) 2014年 JuneWang. All rights reserved.
// #import "DataSource.h" @implementation DataSource
+(DataSource *)sharedDataSource{
static DataSource *dataSource = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
dataSource = [DataSource new];
});
return dataSource;
}
@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

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)的更多相关文章

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

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

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

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

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

    iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...

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

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

  5. iOS 页面间传值 之 单例传值 , block 传值

    ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...

  6. iOS页面间传值的六种方式

    一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传 ...

  7. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  8. iOS页面间传值的一些方式总结

    废话不多说,直接进入主题: 这里要说的方式有6种:1.属性传值 2.block 3.delegate 4.UserDefault 5.单例 6.通知(篇幅原因我只写核心代码,如果看不懂可以直接在最下面 ...

  9. ios页面间跳转方式总结

    转自:http://www.cnblogs.com/anywherego/p/3542202.html 下面以OldViewController(oldC)的按钮btn点击后跳转到NewViewCon ...

随机推荐

  1. BZOJ3993 [SDOI2015]星际战争

    二分答案...然后最大流验证是否可行... 没了,好水啊QAQ /************************************************************** Prob ...

  2. 开源项目导入eclipse的一般步骤[转]

      下载到开源项目后,我们还是希望导入到eclipse中还看,这样要方便点,一般的步骤是这样的 打开源代码目录, 如果看到里面有.calsspath .project文件,那么说明这个项目本来就是ec ...

  3. 目前几款基于html5的前端框架:如Bootstrap、Foundation、Semantic UI 、Amaze UI

    Bootstrap是由Twitter在2011年8月推出的开源WEB前端框架,集合CSS 和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导 ...

  4. Python 2.7教程

    参考:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

  5. UVa 11021 - Tribles

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. python处理url中的中文编码,以及其他编码问题

    1.python中的urlencode与urldecode 2.各种编码转换在线工具 3.python用于url解码和中文解析的小脚本(python url decoder) 4.如何只对url中的中 ...

  7. java程序(一)----HashMap同时获取键值

    快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...

  8. Linux-VLAN

    Why Vlan? VLAN是为解决以太网的广播问题和安全性而提出的一种协议,它在以太网帧的基础上增加了VLAN头,用VLAN ID把用户划分为更小的工作组,限制不同工作组间的用户二层互访,每个工作组 ...

  9. 安装eclipse for c/c++环境

    安装eclipse for c/c++环境:       1.启动eclipse,       2.选择Help->Install New Software...,在Work with的框框下复 ...

  10. shell变量的使用

    转载请标明http://www.cnblogs.com/winifred-tang94/ shell环境中变量有三种类型: a.  环境变量:可以在shell脚本中直接利用“$环境变量名称”的形式引用 ...