iOS页面之间传值的方式(NSUserDefault/Delegate/NSNotification/Block)
来源:互联网 发布:淘宝店铺头像在哪里换 编辑:程序博客网 时间:2024/05/01 10:53
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block)
实现了以下iOS页面间传值:1.委托delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.单例模式方式;6.通过设置属性,实现页面间传值
在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下:
情况1:A页面跳转到B页面
方法:
在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可
@property(nonatomic) NSInteger flag;
//当前<span id="9_nwp" style="width: auto; height: auto; float: none;"><a id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=%CF%B5%CD%B3&k0=%CF%B5%CD%B3&k1=ios&k2=%BF%D8%D6%C6%C6%F7&k3=block&k4=%C8%ED%BC%FE&k5=%C8%ED%BC%FE%BF%AA%B7%A2&sid=61490806c97ccfdf&ch=0&tu=u1704338&jk=1edcfcd1d0eed3ba&cf=29&fv=16&stid=9&urlid=0&luki=7&seller_id=1&di=128" target="_blank" mpid="9" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">系统</span></a></span>标示(0:其他传值方式;1:<span id="10_nwp" style="width: auto; height: auto; float: none;"><a id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=block&k0=block&k1=%C8%ED%BC%FE&k2=%C8%ED%BC%FE%BF%AA%B7%A2&k3=%C6%BB%B9%FB&k4=%CF%B5%CD%B3&k5=ios&sid=61490806c97ccfdf&ch=0&tu=u1704338&jk=1edcfcd1d0eed3ba&cf=29&fv=16&stid=9&urlid=0&luki=3&seller_id=1&di=128" target="_blank" mpid="10" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">block</span></a></span>传值方式)
在A页面的试图控制器中
- (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
@protocol secondViewDelegate
-(
void
)showName:(NSString *)nameString;
@end
设置代理(为防止循环引用,此次采用了weak)
//SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, weak)id<secondViewDelegate> delegate;
@property (nonatomic, copy) a<span id=
"0_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"0_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=block&k0=block&k1=%C8%ED%BC%FE&k2=%C8%ED%BC%FE%BF%AA%B7%A2&k3=%C6%BB%B9%FB&k4=%CF%B5%CD%B3&k5=ios&sid=61490806c97ccfdf&ch=0&tu=u1704338&jk=1edcfcd1d0eed3ba&cf=29&fv=16&stid=9&urlid=0&luki=3&seller_id=1&di=128"
target=
"_blank"
mpid=
"0"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>block</span></a></span> block;
@end
//SecondViewController.m
- (IBAction)delegateMethod:(id)sender {
if
([self notEmpty]) {
[self.delegate showName:self.nameTextField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self showAlert];
}
}
调用,显示
//RootViewController.m
-(
void
)showName:(NSString *)nameString{
self.nameLabel.text = nameString;
}
(2)通过通知notification的方式实现
//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方式实现
分析:
在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;
};
}
链接一篇描述block回调挺有意思的文章:http://blog.csdn.net/mobanchengshuang/article/details/11751671
在查阅资料的过程中,我还看到了以下几种方案:
(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-12-24.
// Copyright (c) 2014年 赵黛阳. 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-12-24.
// Copyright (c) 2014年 赵黛阳. 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
- iOS页面之间传值的方式(NSUserDefault/Delegate/NSNotification/Block)
- IOS学习之页面之间的传值方式(Delegate/NSNotification/Block/NSUserDefault ,单例模式)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault
- iOS开发经验技巧之iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- 三、深入Javascript(1)_看智能社blue老师JS视频整理的笔记
- 友盟(一)了解umeng并添加应用和测试设备信息
- 每天一个linux命令(34):du 命令
- Find Minimum in Rotated Sorted Array
- 怎么处理java.net.BindException: Address already in use: JVM_Bind
- iOS页面之间传值的方式(NSUserDefault/Delegate/NSNotification/Block)
- 对io流学习的简单总结
- 007、第七讲 资源管理:声音文件 总结【unity3d如何添加播放音乐】
- 算法笔记之 全排列算法 一 递归求解
- 文件编程(2)底层文件IO操作
- 使用MFC的CFile文件类和CArchive串行化类进行二进制文件读写操作
- ios tableview cell 侧滑删除
- 每天一个linux命令(36):diff 命令
- 开启博客之旅