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
@property(nonatomic) NSInteger flag; //当前系统标示(0:其他传值方式;1:<span id="13_nwp" style="width: auto; height: auto; float: none;"><a id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=block&k0=block&kdi0=0&luki=5&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" target="_blank" mpid="13" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">block</span></a></span>传值方式)

在A页面的试图控制器

1
2
3
4
5
6
- (IBAction)showSecondView:(id)sender {
     SecondViewController *second = [[SecondViewController alloc] initWithNibName:@ "SecondViewController" bundle:nil];
     second.delegate = self;
     second.<span id= "11_nwp" style= "width: auto; height: auto; float: none;" ><a id= "11_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=flag&k0=flag&kdi0=0&luki=2&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" target= "_blank" mpid= "11" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >flag</span></a></span> = 0;
     [self presentViewController:second animated:YES completion:nil];
}

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

主流方案:

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

设置协议及方法
1
 
1
//SecondViewController.h
1
2
3
@protocol secondViewDelegate
-( void )showName:(NSString *)nameString;
@end

设置代理(为防止循环引用,此次采用了weak)

1
 
1
//SecondViewController.h
1
2
3
4
@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?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=block&k0=block&kdi0=0&luki=5&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" 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
点击按钮传递数组让其显示
1
2
3
4
5
6
7
8
9
//SecondViewController.m
- (IBAction)delegateMethod:(id)sender {
     if ([self notEmpty]) {
         [self.delegate showName:self.nameTextField.text];
         [self dismissViewControllerAnimated:YES completion:nil];
     } else {
         [self showAlert];
     }
}

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

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

在B页面的控制器中,发送通知:
1
2
3
4
5
6
7
8
9
//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页面的控制器中,注册通知:

1
2
3
4
5
6
7
//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];
}

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

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

调用,显示

1
2
3
4
5
6
//RootViewController.m
 
-( void )ChangeNameNotification:(NSNotification*)notification{
     NSDictionary *nameDictionary = [notification userInfo];
     self.nameLabel.text = [nameDictionary objectForKey:@ "name" ];
}

(3)block方式实现

分析:

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

1
2
//SecondViewController.h
typedef void (^ablock)(NSString *str);
1
2
3
//SecondViewController.h
 
@property (nonatomic, copy) ablock block;

在B试图控制器中,

1
2
3
4
5
6
7
8
9
10
- (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

1
2
3
4
5
6
7
- (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来传递

1
2
3
4
5
6
7
8
9
//通过文件或者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试图控制器显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-( 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 *<span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=data&k0=data&kdi0=0&luki=4&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span>Source = [DataSource sharedDataSource];
     if ([dataSource.myName length] != 0) {
         self.nameLabel.text = dataSource.myName;
         dataSource.myName = @"";
     }
*/
}

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

B试图控制器

1
2
3
4
5
6
7
8
9
10
//通过单例方式传值(感觉不太适合此类传值,如果要用单例方式传值的话,可以考虑此方式)
- (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试图控制器显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-( 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 *<span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=data&k0=data&kdi0=0&luki=4&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span>Source = [DataSource sharedDataSource];
     if ([dataSource.myName length] != 0) {
         self.nameLabel.text = dataSource.myName;
         dataSource.myName = @"";
     }
*/
}
@end

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
//  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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
//  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 *<span id= "1_nwp" style= "width: auto; height: auto; float: none;" ><a id= "1_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=ca181de9ee49d360&k=data&k0=data&kdi0=0&luki=4&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=60d349eee91d18ca&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4452%2Ehtml&urlid=0" target= "_blank" mpid= "1" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >data</span></a></span>Source = nil;
     static dispatch_once_t once;
     dispatch_once(&once, ^{
         dataSource = [DataSource new ];
     });
     return dataSource;
}
@end

程序运行截图

A视图:

B视图

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

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值