无法从创建的ViewController代码恢复状态

时间:2019-07-24 18:16:22

标签: swift state-restoration

我正在尝试恢复从代码创建的ViewController。我试图举一个非常简单的例子,但我不明白为什么它不起作用。应用程序通过单击图像来更改视图的背景色,并且应在应用程序停止时保存它。我正在尝试存储和还原Bool属性,并基于它更改视图背景颜色。当我测试它时,我是通过cmd + shift + h将应用程序发送到背景的,然后从xcode停止该应用程序并再次打开它,但是它不会保存所选的背景色。

  1. 我已在AppDelegate中启用了恢复选项
  2. 在ViewController中,我设置了restoreIdentifier和restoreClass
  3. 在ViewController的扩展中采用了协议UIViewControllerRestoration并实现了编码和解码方法
  4. 已实现的ViewController恢复方法

AppDelegate类

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        return true
    }

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

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

}

ViewController类

class ViewController: UIViewController {

    var sunny: Bool = true {
        didSet {
            setColor()
        }
    }

    lazy var colorView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    lazy var changeColor: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Change", for: .normal)
        button.addTarget(self, action: #selector(change), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.restorationIdentifier = "myVC"
        self.restorationClass = ViewController.self
        view.backgroundColor = .white
        setupUI()
    }

    @objc func change() {
        sunny.toggle()
    }

    func setColor() {
        colorView.backgroundColor = sunny == true ? .yellow : .darkGray
    }

}

extension ViewController: UIViewControllerRestoration {

    func setupUI() {
        view.addSubview(colorView)
        NSLayoutConstraint.activate([
            colorView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            colorView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            colorView.widthAnchor.constraint(equalToConstant: view.frame.width),
            colorView.heightAnchor.constraint(equalToConstant: 500)
        ])

        view.addSubview(changeColor)
        NSLayoutConstraint.activate([
            changeColor.topAnchor.constraint(equalTo: colorView.bottomAnchor, constant: 10),
            changeColor.centerXAnchor.constraint(equalTo: colorView.centerXAnchor),
            changeColor.widthAnchor.constraint(equalToConstant: 100),
            changeColor.heightAnchor.constraint(equalToConstant: 45)
        ])

    }

    override func encodeRestorableState(with coder: NSCoder) {
        coder.encode(sunny, forKey: "isSunny")
        super.encodeRestorableState(with: coder)
    }

    override func decodeRestorableState(with coder: NSCoder) {
        guard let isSunny = coder.decodeBool(forKey: "isSunny") as? Bool else {return}
        sunny = isSunny
        setColor()
        super.decodeRestorableState(with: coder)
    }

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

0 个答案:

没有答案