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 *)#>];

四种传值方法(通知、block、属性、NSUserDefaults)的更多相关文章

  1. ASP.NET MVC 四种传值方法

    1.后台传值: public class DataController : Controller { // GET: Data public ActionResult Index() { //1 Vi ...

  2. CSS当中color的四种表示方法

    这是我的第一篇博客,所以写的东西会比较简单. css当中,好多地方都会用到color属性,用来使html内容丰富多彩,例如:background-color:border-color: 第一种表示法使 ...

  3. 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core

    百度地图和高德地图坐标系的互相转换   GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...

  4. javascript DOM和DOM操作的四种基本方法

    在了解了javascript的语言特性后,javascript真正大放光彩的地方来了——这就是javascript DOM Javascript DOM DOM(Document Object Mod ...

  5. C#四种深拷贝方法

    //四种深拷贝方法 public static T DeepCopyByReflect<T>(T obj) { //如果是字符串或值类型则直接返回 if (obj is string || ...

  6. C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏

    C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...

  7. C语言中返回字符串函数的四种实现方法

    转自C语言中返回字符串函数的四种实现方法 其实就是要返回一个有效的指针,尾部变量退出后就无效了. 有四种方式: 1.使用堆空间,返回申请的堆地址,注意释放 2.函数参数传递指针,返回该指针 3.返回函 ...

  8. sass学习笔记 -- sass的四种编译方法

    sass的四种编译方法:(.scss) (一)ruby下的命令行编译 首先需要安装ruby,注意需勾选Add Ruby executables to your PATH选项,以添加环境变量. ruby ...

  9. Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法

    Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...

随机推荐

  1. 前端 ajax 获取后台json数据 解析

    先贴代码 function edit(node) { ).text(); alert(customerid) $.ajax({ type: "post", url: "/ ...

  2. 使用Keras进行多GPU训练 multi_gpu_model

    使用Keras训练具有多个GPU的深度神经网络(照片来源:Nor-Tech.com). 摘要 在今天的博客文章中,我们学习了如何使用多个GPU来训练基于Keras的深度神经网络. 使用多个GPU使我们 ...

  3. android:项目迁移error:Please change caller according to com.intellij.....

    迁移到Android Studio中的项目,在运行时有时会在Event Log中报这种错: Please change caller according to com.intellij.openapi ...

  4. hdfs基本操作-python接口

    安装hdfs包 pip install hdfs 查看hdfs目录 [root@hadoop hadoop]# hdfs dfs -ls -R / drwxr-xr-x - root supergro ...

  5. 用UIControl封装Button

    用UIControl封装Button 效果 说明 UIControl在处理超出触摸范围的触摸事件时有bug 源码 基础类 // // BaseControl.h // BaseControl // / ...

  6. Python(二)列表的增删改查

    一,列表的增删改查 列表中增加元素: 1,从列表的末尾增加一个元素:append("") 2,从列表中插入一个元素:insert(下标位置,插入的元素) 合并列表: 1,name. ...

  7. 审计系统---堡垒机python下ssh的使用

    堡垒机python下ssh的使用 [堡垒机更多参考]http://www.cnblogs.com/alex3714/articles/5286889.html [paramiko的Demo实例]htt ...

  8. Python学习---IO的异步[asyncio +aiohttp模块]

    aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...

  9. python字典去重脚本

    #!/usr/bin/env python # encoding: utf-8 #字典去重小代码 import sys import os import platform try: pass exce ...

  10. docker 自制CentOS 6-lnp镜像

    环境准备 1台centos 6.5镜像虚拟机  febootstrap.docker febootstrap 安装 yum install -y yum-priorities && r ...