首页 技术 正文
技术 2022年11月13日
0 收藏 597 点赞 3,974 浏览 6487 个字

原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html

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页面的试图控制器中

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

//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];
}

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

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

主流方案:

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

【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)– 不错设置协议及方法


//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的方式实现

【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)– 不错在B页面的控制器中,发送通知:【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)– 不错

//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];
}
}

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

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

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

//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];
}

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

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

//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试图控制器中,当输入名字,点击对应的确定按钮后

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

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

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

在A试图显示,回调block

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

- (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;
};
}

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

(4)KVO方式实现

KVO实现原理介绍:http://blog.csdn.net/kesalin/article/details/8194240

在A视图中,编写以下代码

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

//A视图
//一个指向B视图的成员变量
@property (nonatomic, strong) SecondViewController *second;//在A视图跳转到B视图的地方添加如下代码
self.second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.second addObserver:self forKeyPath:@"userName" options:NSKeyValueObservingOptionNew context:nil];
[self presentViewController:self.second animated:YES completion:nil];-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//此处监听key对应值的变化情况
if ([keyPath isEqualToString:@"userName"]) {
self.myLabel.text = self.second.userName;
}
}//清理观察
- (void)dealloc
{
[self.second removeObserver:self forKeyPath:@"userName"];
}

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

在B视图编写以下代码

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

//在B视图
//.h文件@property (nonatomic, strong) NSString *userName;//待监听的成员变量//可以在两处修改userName的值。一个是设置textfield的UITextFieldDelegate。实现一下方法-(void)textFieldDidEndEditing:(UITextField *)textField{
self.userName = self.myField.text;
}//或者在B视图,点击确定按钮,跳转回A视图的时候,修改userName的值也可以- (IBAction)buttonPressed:(id)sender {
self.userName = self.myField.text;
[self dismissViewControllerAnimated:YES completion:nil];
}

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

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

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

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

【转】iOS页面间传值的方式(Delegate/NSNotification/Block/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];
}
}

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

在A试图控制器显示

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

-(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 = @"";
}
*/
}

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

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

B试图控制器

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

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

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

A试图控制器显示

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

-(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

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

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

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

//
// 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 

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

//
// 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

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

程序运行截图

A视图:

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

B视图

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

当输入姓名,并点击对应的确认按钮后,会回到A视图,并显示在B视图中输入的姓名

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

PS:用全局变量的方式也可以实现页面传值的效果。

祝:玩得开心,有什么别的办法或者不正确的地方,欢迎指正。

如果写得不详细,可以通过源码分析。

参考: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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852