【问题标题】:State restoration in storyboard故事板中的状态恢复
【发布时间】:2021-01-12 06:19:43
【问题描述】:

我正在尝试在我的应用程序中保留和恢复某个 VC 的状态,到目前为止,尽管实施(我认为)应该可以解决问题,但我无法强制退出应用程序并恢复状态。我已经浏览了文档和一些示例,但仍在努力让实现工作。我在情节提要中设置了restoreID,并在AppDelegate 中拥有所有必要的功能:

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
    
    // For non-scene-based versions of this app on iOS 13.1 and earlier.
    func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
        coder.encode(1.0, forKey: "version")
        return true
    }

    // For non-scene-based versions of this app on iOS 13.1 and earlier.
    func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
        let version = coder.decodeFloat(forKey: "version")
        if(version == 1.0) {
            return true
        }
        return false
    }
    
    @available(iOS 13.2, *)
    // For non-scene-based versions of this app on iOS 13.2 and later.
    func application(_ application: UIApplication, shouldSaveSecureApplicationState coder: NSCoder) -> Bool {
        coder.encode(1.0, forKey: "version")

        return true
    }
    
    @available(iOS 13.2, *)
    // For non-scene-based versions of this app on iOS 13.2 and later.
    func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
        let version = coder.decodeFloat(forKey: "version")
        if(version == 1.0) {
            return true
        }
        return false
    }

在我的视图控制器中:

extension CurrentSessionVC {
   
    override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        
        coder.encode("Hello World", forKey: "message")
    }
    
    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        
        let message = coder.decodeObject(forKey: "message") as! String
        print(message)
        
    }
}

我设置了一些断点来看看发生了什么,当我多任务并清除应用程序时,shouldSaveSecureApplicationState 中的断点触发,然后当我关闭应用程序时,停止程序并重新运行它@987654326 @ 函数触发,但视图控制器未恢复。我已经完成并测试了一个苹果示例 (https://developer.apple.com/documentation/uikit/uiviewcontroller/restoring_your_app_s_state),它完全按照应有的方式工作,您清除应用程序,下次打开应用程序时,它会从您离开的地方继续。我完全错过了一些重要的事情吗?

【问题讨论】:

    标签: ios swift uistoryboard state-restoration


    【解决方案1】:
    • 通过在 iPhone 模拟器上打开主屏幕来暂停应用程序。您可以向上滑动或使用快捷键:‘Command (⌘) + Shift (⇧) + H’

    !!!不要通过从多任务菜单向上滑动来手动终止应用程序。在这种情况下,系统会删除所有状态信息。

    • 单击“停止”按钮或使用快捷方式停止 Xcode 调试器:“Command (⌘) + .”
    • 使用 Xcode 重新运行应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-01
      • 2014-01-18
      • 2014-09-03
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2016-01-11
      相关资源
      最近更新 更多