四种传值方法(通知、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 *)#>];
四种传值方法(通知、block、属性、NSUserDefaults)的更多相关文章
- ASP.NET MVC 四种传值方法
1.后台传值: public class DataController : Controller { // GET: Data public ActionResult Index() { //1 Vi ...
- CSS当中color的四种表示方法
这是我的第一篇博客,所以写的东西会比较简单. css当中,好多地方都会用到color属性,用来使html内容丰富多彩,例如:background-color:border-color: 第一种表示法使 ...
- 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core
百度地图和高德地图坐标系的互相转换 GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...
- javascript DOM和DOM操作的四种基本方法
在了解了javascript的语言特性后,javascript真正大放光彩的地方来了——这就是javascript DOM Javascript DOM DOM(Document Object Mod ...
- C#四种深拷贝方法
//四种深拷贝方法 public static T DeepCopyByReflect<T>(T obj) { //如果是字符串或值类型则直接返回 if (obj is string || ...
- C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏
C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...
- C语言中返回字符串函数的四种实现方法
转自C语言中返回字符串函数的四种实现方法 其实就是要返回一个有效的指针,尾部变量退出后就无效了. 有四种方式: 1.使用堆空间,返回申请的堆地址,注意释放 2.函数参数传递指针,返回该指针 3.返回函 ...
- sass学习笔记 -- sass的四种编译方法
sass的四种编译方法:(.scss) (一)ruby下的命令行编译 首先需要安装ruby,注意需勾选Add Ruby executables to your PATH选项,以添加环境变量. ruby ...
- Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...
随机推荐
- immutable.js 在React、Redux中的实践以及常用API简介
immutable.js 在React.Redux中的实践以及常用API简介 学习下 这个immutable Data 是什么鬼,有什么优点,好处等等 mark : https://yq.aliyu ...
- js匹配日期和时间的正则表达式
自己写比较头疼,copy下来留着以后用 //日期的正则表达式 -]\d{}-([-]|[-])-([-]|[-][-]|[-])$/; var regExp = new RegExp(reg); if ...
- ES6中的import与export对class操作相关用法举例
两种用法: 一.指定输出类名 // export 输出 export class App extends React.Componet { // ..code } // import 引入 impor ...
- Oracle EBS AR 更新客户组织层
declare -- Local variables here i integer; g_module ) := 'TCA_V2_API'; lrec_org hz_party_v2pub.organ ...
- 基于JVM(内存)和Tomcat性能调优
一.总结前一天的学习 从“第三天”的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü 吞吐量 ü Responsetime ü Cpuload ü MemoryUsa ...
- [翻译] ios-image-filters
ios-image-filters https://github.com/esilverberg/ios-image-filters photoshop-style filter interface ...
- [翻译] snapshotViewAfterScreenUpdates
snapshotViewAfterScreenUpdates This method very efficiently captures the current rendered appearance ...
- Python静态方法实现单实例模式
单实例模式 当程序中需要同一个实例就可以解决问题的场景,可以使用单实例模式
- Python学习---IO的异步[tornado模块]
tornado是一个异步非阻塞的WEB框架.它的异步非阻塞实际上就是用事件循环写的. 主要体现在2点: 1. 作为webserver可以接收请求,同时支持异步处理请求.Django只能处理完成上一个请 ...
- 铁乐学Python_day03-字符串常用操作方法
文:铁乐与猫 2018-3-20 1)字符串首个字母大写,其它字母也会转换成小写: S.capitalize() -> str 记忆方法:capital(大写字母) def capitalize ...