ios 给网页传值_IOS 界面之间传值总结

本文主要介绍iOS开发中页面间跳转传值的问题,归纳了多种传值方式,包括委托、通知、block、UserDefault或文件、单例模式以及设置属性等方式,并给出了详细的代码示例和程序运行截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1.//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];

}

//SecondViewController.h

@protocol secondViewDelegate

-(void)showName:(NSString *)nameString;

@end

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

//SecondViewController.h

@interface SecondViewController : UIViewController

@property (nonatomic, weak)id delegate;

@property (nonatomic, copy) ablock 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;

}

最重要也是最容易忽略的,就是一定要设置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'];

}

//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这个类,存放数据

import

@interface DataSource : NSObject

@property (nonatomic, strong) NSString myName;

+(DataSource)sharedDataSource;

@end````

@implementation DataSource

+(DataSource *)sharedDataSource{

static DataSource *dataSource = nil;

static dispatch_once_t once;

dispatch_once(&once, ^{

dataSource = [DataSource new

];

});

return dataSource;

}

@end````

程序运行截图

A视图:

![\](http://upload-images.jianshu.io/upload_images/1116084-c6846f597e3ff114.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

B视图

![\](http://upload-images.jianshu.io/upload_images/1116084-108396089dfd0a1c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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

![\](http://upload-images.jianshu.io/upload_images/1116084-75df3ddccd8106ef.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值