【发布时间】:2018-03-06 01:18:03
【问题描述】:
我正在使用 Xcode 8 和 Swift 2.3
我浏览了几个网站,它们是有关状态恢复方法的指南:
https://www.raywenderlich.com/117471/state-restoration-tutorial
但我需要在按钮单击时存储和恢复视图控制器的状态并传递已识别的键。
我不能在情节提要中使用单个标识符,因为我需要保存同一个视图控制器的许多实例,因此需要为每个实例使用不同的标识符,根据传递的键标识符,它应该只恢复同一视图控制器的特定实例
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
}
ViewController 代码:
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
}
}
【问题讨论】:
标签: ios swift uiviewcontroller decode encode