我如何使应用状态还原正常工作?

问题描述 投票:0回答:1

我有一个简单的应用程序,其中包含2个表视图控制器,并且我试图在应用程序启动时保留第一个表视图控制器屏幕。从第二个表视图中选择一个单元格时,第一个表视图将动态填充。我一直在测试的方法是添加一个新单元格,将应用程序最小化,单击Xco​​de中的停止,然后按Xcode上的播放按钮。但是它什么也没做,我已经坚持了好几天,而且我不知道自己在做什么错,任何帮助将不胜感激。

我设置了我的restoreId:

 override func viewDidLoad() {
     super.viewDidLoad()

     restorationIdentifier = "MainTableViewController"        
}

这是我的AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.window?.makeKeyAndVisible()
        return true
    }

    // MARK: UISceneSession Lifecycle

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

    func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
        return true
    }

    func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
        return true
    }

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        let viewController = MainTableViewController()
        let navC = UINavigationController(rootViewController: viewController)
        navC.restorationIdentifier = "MainTableViewController"
        window?.rootViewController = navC
        return true

    }

    func application(_ application: UIApplication, didDecodeRestorableStateWith coder: NSCoder) {
        UIApplication.shared.extendStateRestoration()
        DispatchQueue.main.async {
            UIApplication.shared.completeStateRestoration()
        }
    }
}


在我的第一个表视图控制器中,我有这个:

extension MainTableViewController: UIViewControllerRestoration {

    static let activityIdentifierKey = "MainActivity"

    override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        coder.encode(exchangeRates, forKey: MainTableViewController.activityIdentifierKey)
    }

    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        if let returnedPersons = coder.decodeObject(forKey: MainTableViewController.activityIdentifierKey) as? [Person]{
            persons = returnedPersons
        }
    }

    override func applicationFinishedRestoringState() {
        tableView.reloadData()
    }

    static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
        guard let restoredPersons = coder.decodeObject(forKey: "MainActivity") as? [Persons] else {
            print("decoding User Detail")
            return nil
        }

        let vc = MainTableViewController()
        vc.persons = restoredPersons
        return vc
    }
}

我已尝试在iOS 13.2,iOS 12和iOS 11上运行该应用程序

ios swift
1个回答
0
投票

这可能不是代码问题-这只是状态恢复在模拟器中的工作方式。当您在Xcode中停止应用程序时,该应用程序过程突然终止,并且没有时间记录状态信息。

这很痛苦,但是要在模拟器中测试状态恢复,您需要像在硬​​件上一样在模拟器中关闭应用程序:

  1. [在模拟器中,按两次主屏幕按钮(Shift-Cmd-H)。
  2. 您将看到任务切换器。向上拖动您的应用程序以使其关闭。
  3. 以Xcode重新启动应用程序。

您的状态恢复代码现在应该可以使用。

您可以通过在shouldSaveApplicationState中放置一个断点来验证这一点。当您从Xcode停止应用程序时,它不会被调用,但是如果您在模拟器中关闭应用程序,它将被调用。

© www.soinside.com 2019 - 2024. All rights reserved.