zoukankan      html  css  js  c++  java
  • 四种传值方法(通知、block、属性、NSUserDefaults)

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

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

    2、block 反向传值

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

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

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

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

    4、数据持久化传值

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

     NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
        //存储数据
        [userDefaults setObject:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setInteger:<#(NSInteger)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setBool:<#(BOOL)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setURL:<#(nullable NSURL *)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setFloat:<#(float)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setDouble:<#(double)#> forKey:<#(nonnull NSString *)#>];
        [userDefaults setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];
        //读取数据
        [userDefaults objectForKey:<#(nonnull NSString *)#>];
        [userDefaults integerForKey:<#(nonnull NSString *)#>];
        [userDefaults boolForKey:<#(nonnull NSString *)#>];
        [userDefaults URLForKey:<#(nonnull NSString *)#>];
        [userDefaults floatForKey:<#(nonnull NSString *)#>];
        [userDefaults doubleForKey:<#(nonnull NSString *)#>];
        [userDefaults valueForKey:<#(nonnull NSString *)#>];
  • 相关阅读:
    8月面试题目收录
    html5读取本地文件,图片预览
    Identity Server4资料
    vue与Element实际应用参考
    webpack与vue环境搭建(转载)
    .NET使用Bogus生成大量随机数据(转载)
    Docker笔记:常用服务安装——Nginx、MySql、Redis(转载)
    RabbitMQ操作代码封装
    RSA加密与解密
    .NET CORE编写控制台程序应有的优雅姿势(转载)
  • 原文地址:https://www.cnblogs.com/sheer-code/p/10451441.html
Copyright © 2011-2022 走看看