无法使状态还原与编程导航控制器一起使用



我无法使用导航控制器进行状态恢复。我正在使用 Swift,不想使用故事板(程序化(。我在网上找到的几乎所有帮助要么不是在 Swift 中,要么是使用 Storyboard。

在下面的演示代码中,ViewController 包含一个简单的 PickerView,selection变量跟踪选取器选择。应用程序代表有 2 个选项。 对于选项1,不使用导航控制器,并且选取器的状态恢复正常,但不会使用选项 2中的导航控制器还原。(在下面的代码中,选项 1 被注释掉,选项 2 处于活动状态(。

您可以将下面的代码复制并粘贴到一个新的 singleView 应用程序中,它应该重现我所描述的内容。(我测试过(

应用委托代码:

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let pickerVC = ViewController()
//Option 1: No NavC used
//window?.rootViewController = pickerVC
//Option 2: NavC used
let navC = UINavigationController(rootViewController: pickerVC)
navC.restorationIdentifier = "PickerNav"
window?.rootViewController = navC
return true
}
//  func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [Any], coder: NSCoder) -> UIViewController? {
//    let storyboard = UIStoryboard(name: "Main", bundle: nil)
//      if let lastItem = identifierComponents.last as? String {
//        return storyboard.instantiateViewController(withIdentifier: lastItem)
//      }
//    return nil
//  }
}

视图控制器代码:

import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var pickerView: UIPickerView!
var selection = 0
let group = ["Fruit","Vegetable","Meat","Bread"]
override func viewDidLoad() {
super.viewDidLoad()
restorationIdentifier = "PickerVC"
setupPicker()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
pickerView.selectRow(selection, inComponent: 0, animated: false)
}
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(selection, forKey: "selection")
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
selection = coder.decodeInteger(forKey: "selection")
super.decodeRestorableState(with: coder)
}
func setupPicker() {
pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
view.addSubview(pickerView)
pickerView.translatesAutoresizingMaskIntoConstraints = false
pickerView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
pickerView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor).isActive = true
pickerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
pickerView.heightAnchor.constraint(equalToConstant: 300).isActive = true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return group.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selection = pickerView.selectedRow(inComponent: 0)
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.font = UIFont.systemFont(ofSize: 26)
pickerLabel.textAlignment = .center
pickerLabel.text = group[row]
pickerLabel.textColor = .white
return pickerLabel
}
}

我的测试细节:使用选项 1,更改选取器后跟cmd-Shift-H会导致selection变量保存在encodeRestorableState中。然后我单击 XcodeStop按钮,然后再次运行它,selection变量在decodeRestorableState中恢复。相比之下,使用选项 2 状态还原不起作用,因为从不调用decodeRestorableState因此不会还原selection变量。但是,viewDidAppear处的断点显示navigationController?.restorationIdentifier= "PickerNav" 且restorationIdentifier= "PickerVC">

从我所读到的内容来看,我怀疑我可能需要在 AppDelegate 中使用viewControllerWithRestorationIdentifierPath,但我不知道如何正确使用它。 我在 AppDelegate 底部的尝试(代码被注释掉(导致应用程序崩溃。

您可以向 ViewController 添加扩展并使其符合协议UIViewControllerRestoration并实现viewControllerWithRestorationIdentifierPath方法。 在ViewControllerviewDidLoad函数中添加restorationClass就像restorationIdentifer一样

override func viewDidLoad() {
super.viewDidLoad()
restorationIdentifier = "PickerVC"
restorationClass = ViewController.self
setupPicker()
}

extension ViewController: UIViewControllerRestoration {
static func viewController(withRestorationIdentifierPath identifierComponents: [Any], coder: NSCoder) -> UIViewController? {
let vc = ViewController()
return vc
}
}

将此代码添加到 ViewController 类中,并删除在AppDelegate中添加的viewControllerWithRestorationIdentifierPath方法

最新更新