gpt4 book ai didi

ios - 单击按钮时快速存储和恢复 ViewController 状态

转载 作者:行者123 更新时间:2023-11-28 15:18:41 25 4
gpt4 key购买 nike

我正在使用 Xcode 8 和 Swift 2.3

我浏览了几个网站,它们是关于状态恢复方法的指南:

https://www.raywenderlich.com/117471/state-restoration-tutorial

https://github.com/shagedorn/StateRestorationDemo/blob/master/StateRestorationDemo/CityViewController.swift

但我需要通过传递已识别的键来存储和恢复按钮单击时 View Controller 的状态。

我不能在 Storyboard中使用单个标识符,因为我需要保存同一个 View Controller 的多个实例,因此需要为每个实例使用不同的标识符,基于传递的 key 标识符,它应该只恢复同一个 View Controller 的特定实例

func sevNameVccBtnFnc(identifierKey :String)
{
// How to manually call encodeRestorableStateWithCoder
}

func revNameVccBtnFnc(identifierKey :String)->nameVcc
{
// How to manually call decodeRestorableStateWithCoder
}

AppDelegate 代码:

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

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

View Controller 代码:

class nameVcc: UIViewController, UIViewControllerRestoration
{
override func viewDidLoad()
{
super.viewDidLoad()
}

override func encodeRestorableStateWithCoder(coder: NSCoder)
{
print("encodeRestorableStateWithCoder")
super.encodeRestorableStateWithCoder(coder)
}

override func decodeRestorableStateWithCoder(coder: NSCoder)
{
print("decodeRestorableStateWithCoder")
super.decodeRestorableStateWithCoder(coder)
}

static func viewControllerWithRestorationIdentifierPath(identifierComponents: [AnyObject], coder: NSCoder) -> UIViewController?
{
print("viewControllerWithRestorationIdentifierPath")
let vc = nameVcc()
return vc
}
}

最佳答案

But I need to store and restore the state of the view controller on button click with passing identified key.

没有。这不能通过单击按钮来完成。 UIKit 提供编码器仅在应用程序进入 BG 模式时对您的数据进行编码。很可能无法手动调用这些方法。状态保存/恢复不是为此设计的,或者至少 UIKit 目前不允许这样做。您可能必须自己编写自己的状态恢复机制,例如 UserDefaults 或写入磁盘。

关于ios - 单击按钮时快速存储和恢复 ViewController 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com