iOS界面之间的传值
2015-10-13 16:50
537 查看
iOS实现页面间传值的方式:
通过设置属性,实现页面间传值
委托
通知
单例模式方式
使用
本篇教程中,有两个视图控制器,分别为
一.通过设置属性,实现页面间传值(适合单向传值)
AnotherController.h的代码
AnotherController.m的代码
运行图示:
总结:这一方式是通过在控制器中设置属性来接收传过来的值,不足就是设置的属性要暴露出来。
二.委托
使用委托模式(在两个类之间)是可以远程传值的,
AnotherController.h代码
AnotherController.m代码
ViewController.m代码:
运行效果如图:
开启应用时效果图如下:
跳转到第二个界面,用来获取值:
返回第一个界面,利用协议中的方法来接收到从第二个界面传递过来的值:
注意:
第一个界面接收第二个界面传递过来的值,是通过以下的代理方法来实现的:
也就是在第二个界面中远程调用该方法,警记:此时的
如果调用远程方法时,远程方法体中用到的控件还没有初始化,值为
三.通过通知
在AnotherController.m代码中,点击按钮发送通知且切换到第一个界面:
在ViewController.m代码中注册通知,并且在接收到通知时,调用指定方法:
四.通过block方式实现
在
在
在
通过设置属性,实现页面间传值
委托
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]; }
相关文章推荐
- [iOS]应用内切换语言,本地化,国际化
- IOS多线程
- iOS远程推送
- IOS推送声音
- iOS简单的抖动动画效果
- iOS9 适配中遇到的问题记录
- -webkit-overflow-scrolling:touch iosBug
- iOS开发教程:Storyboard全解析
- 集成IOS 环信SDK
- ios- block循环引用问题
- ios- block循环引用问题
- iOS Xcode7免证书真机调试
- ShareSDK for IOS 的个人理解
- IOS开发之格式化日期时间
- iOS文件操作两种方式:NSFileManager和流操作
- IOS 在开发中使用KVO观测属性变化
- iOS 去掉导航栏返回按钮的文字
- 要缩小通过两个触摸点的观点(iOS)
- IOS中简单的KVC概念理解与应用
- ios--apns Device token的获得和改变详解