汇总ios开发逆向传值的方法
iOS的逆向传值有很多种方法,下面来总结几种常用的传值方式(只贴相关代码):
第一种:代理传值
第二个控制器:
@protocol WJSecondViewControllerDelegate <NSObject>
- (void)changeText:(NSString*)text;
@end
@property(nonatomic,assign)id<WJSecondViewControllerDelegate>delegate;
- (IBAction)buttonClick:(UIButton*)sender {
_str = sender.titleLabel.text;
[self.delegate changeText:sender.titleLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}
第一个控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
svc.delegate = self;
svc.str = self.navigationItem.title;
[self.navigationController pushViewController:svc animated:YES];
[svc release];
}
- (void)changeText:(NSString *)text{
self.navigationItem.title = text;
}
第二种:通知传值
第一个控制器:
//注册监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
- (void)limitDataForModel:(NSNotification *)noti{
self.gamesInfoArray = noti.object;
}
第二个控制器:
//发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"NOV" object:gameArray];
第三种:单例传值
Single是一个单例类,并且有一个字符串类型的属性titleName
在第二个控制器:
- (IBAction)buttonClick:(UIButton*)sender {
Single *single = [Single sharedSingle];
single.titleName = sender.titleLabel.text;
[self.navigationController popViewControllerAnimated:YES];
}
第一个控制器:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
Single *single = [Single sharedSingle];
self.navigationItem.title = single.titleName;
}
第四种:block传值
第二个控制器:
@property (nonatomic,copy) void (^changeText_block)(NSString*);
- (IBAction)buttonClick:(UIButton*)sender {
_str = sender.titleLabel.text;
self.changeText_block(sender.titleLabel.text);
[self.navigationController popViewControllerAnimated:YES];
}
第一个控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
svc.str = self.navigationItem.title;
[svc setChangeText_block:^(NSString *str) {
>self.navigationItem.title = str;
}];
[self.navigationController pushViewController:svc animated:YES];
}
第五种:extern传值
第二个控制器:
extern NSString *btn;
- (IBAction)buttonClick:(UIButton*)sender {
btn = sender.titleLabel.text;
[self.navigationController popViewControllerAnimated:YES];
}
第一个控制器:
NSString *btn = nil;
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationItem.title = btn;
}
第六种:KVO传值
第一个控制器:
- (void)viewDidLoad {
[super viewDidLoad];
_vc =[[SecondViewController alloc]init];
//self监听vc里的textValue属性
[_vc addObserver:self forKeyPath:@"textValue" options:0 context:nil];
}
第二个控制器:
- (IBAction)buttonClicked:(id)sender {
self.textValue = self.textField.text;
[self.navigationController popViewControllerAnimated:YES];
}
其实还有很多种传值方式,比如说NSUserDefaults,先把数据保持在本地,再读取,或者写入plist及其它类型的文件再读取等等许多方式,在这里就不一一列举了!这些代码写的时间比较久了,今天整理了一下,还比较乱,有什么不对或不足的地方请见谅!
相关文章
Xcode 9下适配iPhoneX导致iOS 10不兼容问题的解决方法
这篇文章主要给大家介绍了关于Xcode 9下适配iPhoneX导致iOS 10不兼容问题的解决方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2018-04-04
iOS9 系统分享调用之UIActivityViewController
UIActivityViewController类是一个标准的view controller,通个使用这个controller,你的应用程序就可以提供各种服务。本文给大家介绍iOS9 系统分享调用之UIActivityViewController,感兴趣的朋友一起学习吧2015-11-11
NSURLSession跨域重定向透传HTTP Header问题解决
这篇文章主要为大家介绍了NSURLSession跨域重定向透传HTTP Header问题解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-11-11
iOS App开发中Core Data框架基本的数据管理功能小结
除了使用SQL关系型数据库,我们还可以使用Xcode中提供的Core Data来进行表结构数据处理,这里我们就来初步整理iOS App开发中Core Data框架基本的数据管理功能小结:2016-06-06


最新评论