2017-09-25 83 views
0

我使用的Xcode 8和雨燕2.3斯威夫特保存和恢复的ViewController状态点击

我通过几个网站,这是关于状态恢复方法导游去:

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

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

但我需要存储和恢复视图控制器的状态按钮点击传递确定的关键。

我不能在故事板中使用单一标识符,因为我需要保存同一个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 
    } 
} 

回答

1

但我需要存储和传递与认定的关键还原上 按钮单击视图控制器的状态。

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