Swift 单击按钮时存储和恢复 ViewController 状态
Swift Store and Restore ViewController state on Button Click
我正在使用 Xcode 8 和 Swift 2.3
我浏览了几个网站,它们是关于状态恢复方法的指南:
https://www.raywenderlich.com/117471/state-restoration-tutorial
但我需要通过传递已识别的键来存储和恢复按钮单击时视图控制器的状态。
我不能在情节提要中使用单个标识符,因为我需要保存多个相同的实例 viewcontroller,因此需要为每个实例使用不同的标识符,基于传递的关键标识符,它应该只恢复特定的同一视图控制器的实例
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
}
}
But I need to store and restore the state of the view controller on
button click with passing identified key.
没有。这不能通过单击按钮来完成。 UIKit 提供编码器仅在应用程序进入 BG 模式时对您的数据进行编码。很可能无法手动调用这些方法。 State Preservation/Restoration 不是为此设计的,或者至少 UIKit 目前不允许这样做。您可能必须自己编写自己的状态恢复机制,例如 UserDefaults
或写入磁盘。
我正在使用 Xcode 8 和 Swift 2.3
我浏览了几个网站,它们是关于状态恢复方法的指南:
https://www.raywenderlich.com/117471/state-restoration-tutorial
但我需要通过传递已识别的键来存储和恢复按钮单击时视图控制器的状态。
我不能在情节提要中使用单个标识符,因为我需要保存多个相同的实例 viewcontroller,因此需要为每个实例使用不同的标识符,基于传递的关键标识符,它应该只恢复特定的同一视图控制器的实例
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
}
}
But I need to store and restore the state of the view controller on button click with passing identified key.
没有。这不能通过单击按钮来完成。 UIKit 提供编码器仅在应用程序进入 BG 模式时对您的数据进行编码。很可能无法手动调用这些方法。 State Preservation/Restoration 不是为此设计的,或者至少 UIKit 目前不允许这样做。您可能必须自己编写自己的状态恢复机制,例如 UserDefaults
或写入磁盘。