您的位置:首页 > 移动开发 > IOS开发

iOS界面之间的传值

2015-10-13 16:50 537 查看
iOS实现页面间传值的方式:

通过设置属性,实现页面间传值

委托
delegate
方式

通知
notification
方式

block
方式

UserDefaults
或者文件方式

单例模式方式

使用
SharedApplication
,定义一个变量来传递

本篇教程中,有两个视图控制器,分别为
ViewController
AnotherController
,我们想把
ViewController
UITextField
的值传给
AnotherController
中的
UILabel


一.通过设置属性,实现页面间传值(适合单向传值)

ViewController.m
中的代码

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

AnotherController* another=[[AnotherController alloc] init];
another.value=self.valueText.text;
[self  presentViewController:another animated:YES completion:nil];

}


AnotherController.h的代码

@property(nonatomic,strong)NSString* value;


AnotherController.m的代码

-(void)viewWillAppear:(BOOL)animated{
self.valueLabel.text=self.value;
}


运行图示:





总结:这一方式是通过在控制器中设置属性来接收传过来的值,不足就是设置的属性要暴露出来。

二.委托
delegate
方式(两个页面双向传值)

使用委托模式(在两个类之间)是可以远程传值的,

AnotherController.h代码

@protocol AnotherDelegate <NSObject>

-(void)passValue:(NSString*) value;

@end

@interface AnotherController : UIViewController

@property(nonatomic,weak)id<AnotherDelegate> delegate;

@end


AnotherController.m代码

@interface AnotherController ()

@property(nonatomic,weak)UITextField* valueText;

@end

@implementation AnotherController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];

UITextField* valueText=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
[self.view addSubview:valueText];
self.valueText=valueText;
self.valueText.backgroundColor=[UIColor whiteColor];

UIButton* btn=[[UIButton alloc] init];
btn.frame=CGRectMake(100, 240, 200, 40);
btn.backgroundColor=[UIColor redColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(sendDelegate) forControlEvents:UIControlEventTouchUpInside];

}

-(void)sendDelegate{
if ([self.delegate respondsToSelector:@selector(passValue:)]) {
[self.delegate passValue:self.valueText.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}


ViewController.m代码:

@interface ViewController ()<AnotherDelegate>

@property(nonatomic,weak)UILabel* valueLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];

UILabel* valueLabel=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)];
[self.view addSubview:valueLabel];

self.valueLabel=valueLabel;
self.valueLabel.backgroundColor=[UIColor purpleColor];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

AnotherController* another=[[AnotherController alloc] init];
another.delegate=self;
[self presentViewController:another animated:YES completion:nil];

}

-(void)passValue:(NSString *)value{
self.valueLabel.text=value;
}


运行效果如图:

开启应用时效果图如下:



跳转到第二个界面,用来获取值:



返回第一个界面,利用协议中的方法来接收到从第二个界面传递过来的值:



注意:

第一个界面接收第二个界面传递过来的值,是通过以下的代理方法来实现的:

-(void)passValue:(NSString *)value{
self.valueLabel.text=value;
}


也就是在第二个界面中远程调用该方法,警记:此时的
self.valueLabel
是不为
nil
的,因为在进入第二个界面之前,我们已经完成了对第一个界面的初始化了,所以此时
self.valueLabel
是存在的。

如果调用远程方法时,远程方法体中用到的控件还没有初始化,值为
nil


三.通过通知
notification
的方式实现



AnotherController.m代码中,点击按钮发送通知且切换到第一个界面:

[btn addTarget:self action:@selector(passValue) forControlEvents:UIControlEventTouchUpInside];

-(void)passValue{

[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"name":self.valueText.text}];
[self dismissViewControllerAnimated:YES completion:nil];
}


ViewController.m代码中注册通知,并且在接收到通知时,调用指定方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];

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


四.通过block方式实现

AnotherController.h
中的代码:

#import <UIKit/UIKit.h>

typedef void (^ablock)(NSString* str);//定义一个代码块变量类型

@interface AnotherController : UIViewController

@property(nonatomic,copy)ablock block;//定义一个代码块变量

@end


AnotherController.m
中的代码:

-(void)passValue{
self.block(self.valueText.text);
[self dismissViewControllerAnimated:YES completion:nil];
}


ViewController.m
中的代码:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

AnotherController* another=[[AnotherController alloc] init];
another.block=^(NSString* value){
self.valueLabel.text=value;
};
[self presentViewController:another animated:YES completion:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: