iOS应用状态保存和恢复
当应用被后台Kill掉的时候希望从后台返回的时候显示进入后台之前的内容
在Appdelegate中设置
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{
return YES;
}
为每个ViewController设置:restorationIdentifier(可以直接在sb中设置),restorationClass
如果设置了restorationClas则必须遵守UIViewControllerRestoration协议返回恢复时的控制器
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray<NSString *> *)identifierComponents coder:(NSCoder *)coder{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *controller = [storybord instantiateViewControllerWithIdentifier:@"mywebcontroller"];
controller.restorationIdentifier = [identifierComponents lastObject];
controller.restorationClass = [MyWebViewController class];
return controller;
}
如果未指定代理方法则从Appdelgate中的代理中返回
If a view controller has an associated restoration class, the viewControllerWithRestorationIdentifierPath:coder: method of that class is called during state restoration. That method is responsible for returning the view controller object that matches the indicated view controller. If you do not specify a restoration class for your view controller, the state restoration engine asks your app delegate to provide the view controller object instead.
在控制器中保存和恢复状态
//只保存状态,如果是数据的话保存在文件中如NSUserdefault等
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{
//保存开关状态
[coder encodeBool:_switchBtn.isOn forKey:@"ison"]; [super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{
[super decodeRestorableStateWithCoder:coder];
//获取开关状态
_switchBtn.on = [coder decodeBoolForKey:@"ison"]; }
iOS应用状态保存和恢复的更多相关文章
- Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...
- Android学习总结——Activity状态保存和恢复
Android中启动一个Activity如果点击Home键该Activity是不会被销毁的,但是当进行某些操作时某些数据就会丢失,如下: Java class: package com.king.ac ...
- vim状态保存跟恢复
当我们结束了一天的工作的时候,可能手头的工作仅仅进行了一半,比如我们正在用vim修改一个android 问题,我们定位了问题关键,牵扯到了好几个类,如果这时候我们直接把vim关闭了,那我们下次还要重新 ...
- delphi IOS 后台状态保存
FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...
- 【Android】11.2 通过重写对应的方法保存和恢复实例的状态
分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 通过重写(也叫回调)对应的方法来管理Activity的生命周期,比如用户旋转屏幕时应用程序要能自动保存和恢复实例的 ...
- 如何禁止App在后台运行以及如何保存和恢复App的状态
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...
- Android之Activity状态的保存和恢复
系统在某些情况下会调用onSaveInstanceState()和onRestoreInstanceState() 这两个方法来保存和恢复Activity的状态. 一句话:Activity在" ...
- Android课程---Activity中保存和恢复用户状态
onSaveInstanceState 保存 在暂停之后和保存之前调用 onRestoreInstanceState 恢复 再启动之后和显示之前调用 package com.example.chens ...
- ios开发——实用技术篇&数据保存于恢复
数据保存于恢复 用户操作(输入数据)之后,应用程序退出并且终止之后,当用户再次打开应用的时候还是保持原来的状态 一:在storyBoard中设置恢复标志符 二:在AppDalegate中代理方法 -( ...
随机推荐
- Dictionary导致IIS CPU 100%案例分析 学会使用WinDbg工具
.NET 开发注意 线程安全性问题.弄不好可能会导致CPU满载 特别主要 Dictionary作为静态变量使用的情况. 解决方法: Dictionary 换成 ConcurrentDictiona ...
- ThreadLocal:的面试
ThreadLocal: 为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变 ...
- bean名称相同冲突Annotation-specified bean name 'xx' for bean class [xxx] conflicts with existing, non-compatible bean definition of same name and class[xxx]
工程中引入其他工程的包,由于两个工程中有重名的两个bean,导致在启动时提示如下错误: 根据bean名称在ide中查找,找到这两个重名的类,可以看到由于这两个类使用@Service标注,此时如果不使用 ...
- 搭建EOS全节点并接入主网
#EOS编译安装 #!/bin/bash #install.eos.sh #EOS 安装 #从github克隆主网代码 cd /opt git clone https://github.com/EOS ...
- 30段极简Python代码
Python 是机器学习最广泛采用的编程语言,它最重要的优势在于编程的易用性.如果读者对基本的 Python 语法已经有一些了解,那么这篇文章可能会给你一些启发.作者简单概览了 30 段代码,它们都是 ...
- 数据库事务和spring事务的区别
数据库事务和spring事务 本质上其实是同一个概念,spring的事务是对数据库的事务的封装,最后本质的实现还是在数据库,假如数据库不支持事务的话,spring的事务是没有作用的.数据库的事务说简单 ...
- Excel 相当名称,值相加
药品名称相同,数量汇总 原始表格,同一个名称,存在不同的单位,所以合并处理成一列 将列表复制一份出去,并删除A表重复数据 求和公式=SUMIF(A2:A16,E2:E8,B2:B16) 最后再将药品名 ...
- CentOS7.4安装ipython
摘自:https://www.jianshu.com/p/aa93a0341c82 使用pip3安装ipython及其依赖包:pip3 install ipython在 .bashrc文件中添加环境变 ...
- 报错:sqoop2执行job时:Exception: Job Failed with status:3
报错背景: 创建完成sqoop2的一个job,主要功能是将数据从hdfs存到mysql数据库中. 执行job的时候发生报错. 报错现象: sqoop:> start job -j -s Subm ...
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...