在按钮上快速存储和还原ViewController状态单击 [英] Swift Store and Restore ViewController state on Button Click

查看:108
本文介绍了在按钮上快速存储和还原ViewController状态单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 8和Swift 2.3



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



https://www.raywenderlich.com/117471/state-恢复教程



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



但是我需要



我无法在情节提要中使用单个标识符,因为我需要保存同一viewcontroller的许多实例,并在单击时存储并恢复视图控制器的状态。



,因此需要为每个实例使用不同的标识符,根据传递的键标识符,它应仅还原s的特定实例。 ame视图控制器

  func sevNameVccBtnFnc(identifierKey:String)
{
//如何手动调用encodeRestorableStateWithCoder
}

func revNameVccBtnFnc(identifierKey:String)-> nameVcc
{
//如何手动调用encodeRestorableStateWithCoder
}

AppDelegate代码:

  func应用程序(应用程序:UIApplication,shouldSaveApplicationState编码器:NSCoder)->布尔
{
返回true
}

函数应用程序(应用程序:UIApplication,shouldRestoreApplicationState编码器:NSCoder)->布尔
{
返回true
}

ViewController代码:

 类名Vcc:UIViewController,UIViewControllerRestoration 
{
覆盖func viewDidLoad()
{
super.viewDidLoad()
}

覆盖函数func encodeRestorableStateWithCoder(coder:NSCoder)
{
print( encodeRestorableStateWithCoder)
super.encodeRestorableStateWithCoder(coder)
}

覆盖函数func encodeRestorableStateWithCoder(coder:NSCoder)
{
print( decodeRestorableStateWithCoder)
super.decodeRestorableStateWithCoder(coder)
}

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


解决方案


但是我需要存储和恢复视图控制器的状态在
按钮上单击,并传递已标识的密钥。


不是。单击按钮无法完成此操作。 UIKit提供了仅当应用程序进入BG模式时才对数据进行编码的编码器。最有可能无法手动调用这些方法。并非为此设计状态保存/恢复,或者至少UIKit暂时不允许这样做。您可能需要自己编写自己的状态恢复机制,例如 UserDefaults 或写入磁盘。


I am using Xcode 8 and Swift 2.3

I went through couple of websites, which are guides about state restoration methods:

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

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

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

I cant use single identifier in storyboard because I need to save many instances of the same viewcontroller, so need to use different identifier for each instance, based on the key identifier passed it should restore only that particular instance of the same view controller

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

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

AppDelegate Code:

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

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

ViewController Code:

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.

Nope. This cannot be done on a button click. UIKit provides the coder to encode your data only when they the app is entering BG mode. There is most possibly no way to manually call those methods. State Preservation/Restoration is not designed for that, or at least UIKit does not allow this for now. You may have to write your own State Restoring mechanism yourself with, for example UserDefaults or writing to disk.

这篇关于在按钮上快速存储和还原ViewController状态单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆