`

IOS之UI状态保持与恢复

 
阅读更多

为了实现点击Home键使程序退出,可以在设置属性*-info.plist修改Application does not run in background属性值为YES

为实现UI的状态保持和恢复,包括APP层面和storyboard层面,首要条件就是需要在AppDelegate.m文件添加以下两个方法。

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
    return YES;
}

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
    return YES;
}

 1.APP层面

          在APPDelegate.m中添加如下代码:

//本例子为记录上一次退出的时间,在下一次打开时显示出来
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
    // 获取系统当前时间
    NSDate *date = [NSDate date];
    NSTimeInterval sec = [date timeIntervalSinceNow];
    NSDate *currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
    
    //设置时间输出格式:
    NSDateFormatter *df = [[NSDateFormatter alloc] init ];
    [df setDateFormat:@"yyyy年MM月dd日 HH小时mm分ss秒"];
    NSString *na = [df stringFromDate:currentDate];
    
    
    [coder encodeObject:na forKey:@"lastShutdownTime"];
    
    NSLog(@"application willEncodeRestorableStateWithCoder 时间为:%@", na);
    
    }

- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
    NSString * currentTime = [coder decodeObjectForKey:@"lastShutdownTime"];
    AppUtils *appUtils = [AppUtils alloc];
    if (currentTime) {
        [appUtils showDialog:@"上次关闭时间" message:currentTime];
    }
}


//附带AppUtils类文件
//AppUtils.h
#import <Foundation/Foundation.h>

@interface AppUtils : NSObject

- (void)showDialog:(NSString *)title message:(NSString *)message;

@end

//AppUtils.m
#import "AppUtils.h"

@implementation AppUtils

- (void)showDialog:(NSString *)title message:(NSString *)message{
    UIAlertView *alert = [[UIAlertView  alloc]
                                        initWithTitle:title
                                        message:message
                                        delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
    [alert show];
}

@end

 2.storyboard层面

(1)选择指定的storyboard的ViewController,将其restorationID设置为viewController



 

(2)添加下面的方法,实现退出时保存文本框的文本信息。

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"encodeRestorableStateWithCoder");
    [super encodeRestorableStateWithCoder:coder];
    [coder encodeObject:self.editText.text forKey:@"kSaveKey"];
}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"decodeRestorableStateWithCoder");
    [super decodeRestorableStateWithCoder:coder];
    self.editText.text = [coder decodeObjectForKey:@"kSaveKey"];
}

 

  • 大小: 149.7 KB
  • 大小: 262.1 KB
分享到:
评论

相关推荐

    sidebar-ios14:如何使用新的iOS 14边栏UI元素的示例

    在常规模式和紧凑模式之间切换时,恢复视图控制器的状态。 在紧凑版式和iPhone上恢复为基于标签栏的导航。已知错误以紧凑模式启动应用程序,然后将其更改为常规模式会导致以下错误: Terminating app due to ...

    IOS 实现一个死锁导致 UI 假死的例子

    IOS 实现一个死锁导致 UI 假死的例子 现象 当 APP 启动一段时间后(约半小时左右),经常会发现 App 界面出现“冻死”的现象。同时后台输出: [CocoaGoPush]WorkThreadProc end 这时 App 呈现“假死”状态,点击...

    OBJECTIVE-C编程之道 IOS设计模式解析电子书+源代码

    对象状态第23章 备忘录23.1 何为备忘录模式23.2 何时使用备忘录模式23.3 在TouchPainter中使用备忘录模式23.3.1 涂鸦图的保存23.3.2 涂鸦图的恢复23.3.3 ScribbleMemento的设计与实现23.4 Cocoa Touch框架中的备忘录...

    AXURE9最新版,小版本号3646,序列号可用(win版本)

    2. 页面样式新增移动端尺寸,包含了安卓与IOS主流机型尺寸。 3. 画布取消了滚动栏(可能有些同学不适应了),增加负屏显示。 4. 帮助菜单栏新增Axure官方论坛。 5. 优化浏览器中原型显示效果,新增自适应浏览器宽度...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的应用十分广泛 Webview WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页 Activity ...

    Cocos2D-X游戏开发技术精解

    2.2.3 iOS开发环境 35 2.3 引擎中的混合编译 38 2.3.1 Java与C++的混合编译 38 2.3.2 Objective-C与C++的混合编译 41 2.4 引擎的启点 42 2.4.1 应用程序入口 43 2.4.2 引擎应用入口 44 2.5 丰富的示例程序 46 2.5.1 ...

Global site tag (gtag.js) - Google Analytics