经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
四种传值方法(通知、block、属性、NSUserDefaults)
来源:cnblogs  作者:纯粹的code  时间:2019/3/1 9:13:37  对本文有异议

1、 通知传值-一般常用于返回界面的时候,把返回前界面的值传到返回后界面。

  1. //前一个界面
  2. //注册通知
  3. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"notification" object:nil];
  4. // Do any additional setup after loading the view.
  5. }
  6. //执行通知方法
  7. - (void) notification:(NSNotification *)notifi{
  8. NSLog(@"++++++");
  9. NSLog(@"%@",notifi.userInfo);
  10. }
  11. //移除通知
  12. -(void) dealloc{
  13. //第一种 移除该控制器所有的通知
  14. [[NSNotificationCenter defaultCenter] removeObserver:self];
  15. //第二种 移除该控制器下名为"notification"的通知
  16. // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification" object:nil];
  17. }
  1. //后一个界面
  2. //创建传值信息
  3. NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:@"val1",@"key1",@"val2",@"key2", nil];
  4. //创建通知
  5. NSNotification *notification=[NSNotification notificationWithName:@"notification" object:nil userInfo:dict];
  6. //通过通知中心发送通知
  7. [[NSNotificationCenter defaultCenter] postNotification:notification];

2、block 反向传值

前一个界面获取后一个界面传过来的值

  1. //后一个界面
  2. .h文件
  3. @interface jViewController : UIViewController
  4. //定义block
  5. @property (nonatomic,copy) void (^NextViewControllerBlock)(NSString *tfText);
  6. @end
  7. .m文件
  8. -(void) btnClick:(UIButton *)btn{
  9. //判断block是否为空
  10. if (self.NextViewControllerBlock) {
  11. self.NextViewControllerBlock(@"我是后一个界面传的值");
  12. }
  13. [self.navigationController popViewControllerAnimated:YES];
  14. }
  1. //前一个界面
  2. //.m文件
  3. -(void) btnClick:(UIButton *)btn{
  4. jViewController *nextVC = [[jViewController alloc]init];
  5. nextVC.NextViewControllerBlock = ^(NSString *tfText){
  6. NSLog(@"++++%@",tfText);
  7. };
  8. [self.navigationController pushViewController:nextVC animated:YES];
  9. }

3、属性传值(一般适用于前一个界面传值给后一个界面)

  1. //后一个界面
  2. //首先在.h定义获值属性(就是要有接收传值过来的属性)
  3. @interface jViewController : UIViewController
  4. @property( copy,nonatomic)NSString *str;
  5. @end
  6.  
  7. //.m
  8. -(void) btnClick:(UIButton *)btn{
  9. NSLog(@"++++%@",_str);
  10. }
  1. //前一个界面
  2. -(void) btnClick:(UIButton *)btn{
  3. //跳转到后一个界面,也把值传过去
  4. jViewController *nextVC = [[jViewController alloc]init];
  5. nextVC.str=@"我是第一个界面传给第二个界面的值";
  6. [self.navigationController pushViewController:nextVC animated:YES];
  7. }

4、数据持久化传值

NSUserDefaults是数据持久化的一种主要做存储使用。

  1. NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
  2. //存储数据
  3. [userDefaults setObject:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];
  4. [userDefaults setInteger:<#(NSInteger)#> forKey:<#(nonnull NSString *)#>];
  5. [userDefaults setBool:<#(BOOL)#> forKey:<#(nonnull NSString *)#>];
  6. [userDefaults setURL:<#(nullable NSURL *)#> forKey:<#(nonnull NSString *)#>];
  7. [userDefaults setFloat:<#(float)#> forKey:<#(nonnull NSString *)#>];
  8. [userDefaults setDouble:<#(double)#> forKey:<#(nonnull NSString *)#>];
  9. [userDefaults setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];
  10. //读取数据
  11. [userDefaults objectForKey:<#(nonnull NSString *)#>];
  12. [userDefaults integerForKey:<#(nonnull NSString *)#>];
  13. [userDefaults boolForKey:<#(nonnull NSString *)#>];
  14. [userDefaults URLForKey:<#(nonnull NSString *)#>];
  15. [userDefaults floatForKey:<#(nonnull NSString *)#>];
  16. [userDefaults doubleForKey:<#(nonnull NSString *)#>];
  17. [userDefaults valueForKey:<#(nonnull NSString *)#>];

 

原文链接:http://www.cnblogs.com/sheer-code/p/10451441.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号