我是靠谱客的博主 结实宝马,最近开发中收集的这篇文章主要介绍iOS 界面传值的六种传值方式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

实际开发场景中,页面之间传值非常的普遍,今天就来谈一谈ios 页面传值的六种方式。

页面传值也是指ViewController 传值。

1,属性传值

    顾名思义,在要跳转的页面设置一个属性,由页面1 跳转把值带过去即可。 


NextViewController* nextVc = [[NextViewController alloc]init];
nextVc.str = @"这是属性传值";
[ self presentViewController:nextVc animated:YES completion:nil];

 2,单例传值

  创建一个单例对象,把数据放在单例对象里,启动第二个页面的时候,保存。第二个页面获取就行。

单例对象DefaultInstance.h文件:

@interface DefaultInstance : NSObject
@property(nonatomic,strong)NSString* str;
+ (instancetype)sharedInstance;
@end

单例对象DefaultInstance.m文件

#import "DefaultInstance.h"
@implementation DefaultInstance
//通过类方法创建单例对象
+ (instancetype)sharedInstance{
static DefaultInstance * sharedVC = nil;
if(sharedVC == nil){
sharedVC = [[DefaultInstance alloc]init];
}
return sharedVC;
}
@end

 启动下个页面:


DefaultNextViewController* nextVc = [[DefaultNextViewController alloc]init];
[DefaultInstance sharedInstance].str = @"这是单例传值";
[ self.navigationController pushViewController:nextVc animated:YES];

3,NSUserDefaults 传值

和单例传值很像,只不过 单例是写到内存里,NSUserDefaults 是写到 沙盒文件里。

示例如下:

保存数据启动下一个页面


[[NSUserDefaults standardUserDefaults]setObject:_uiLabel.text forKey:@"intent"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self.navigationController pushViewController:defaultNext animated:YES];

获取数据:

 _uiTextField.text = [[NSUserDefaults standardUserDefaults]stringForKey:@"intent"];

4,代理传值

委托方需要创建一个代理,让代理方实现其方法,达到传值的目的。

委托方:

//委托方创建一个协议
@protocol pathValue <NSObject>
//定义一个方法
- (void)passValue:(NSString*)str;
@end
@interface DelegateNextViewController : UIViewController
@property(nonatomic,strong)NSString* sr;
//定义一个持有协议的id 指针
@property(weak)id<pathValue>delegate;
@end

代理方:

@interface DelegateViewController ()<pathValue>

 实现其方法:

-(void)passValue:(NSString *)str{
_uiLabel.text = str;
}

 启动 下一个页面,并且设置代理 


DelegateNextViewController* delegateNextVc = [[DelegateNextViewController alloc]init];
delegateNextVc.delegate = self;
[self.navigationController pushViewController:delegateNextVc animated:YES];

反向传值:


[self.delegate passValue:@"这是反向传值"];
[self.navigationController popViewControllerAnimated:YES];

5,block 传值

定义block:

@property(copy)void (^block)(NSString*);

启动设置block:


BlockNextViewController* blockNextVc = [[BlockNextViewController alloc]init];
blockNextVc.block = ^(NSString* str){
self->_uiLabel.text = str;
};
[self.navigationController pushViewController:blockNextVc animated:YES];

反向传值:


self.block(self.uiTextField.text);
[self.navigationController popViewControllerAnimated:YES];

6,通知传值

通过发送一个通知消息来传值

注册通知:

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

 实现方法:

- (void)notHandle:(NSNotification*) not{
self.title = not.userInfo[@"notHandle"];
}

发送消息:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil userInfo:@{@"notHandle":self.uiTextField.text}];

demo:

ios 传值

最后

以上就是结实宝马为你收集整理的iOS 界面传值的六种传值方式的全部内容,希望文章能够帮你解决iOS 界面传值的六种传值方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部