![](/img/trans.png)
[英]Opening ViewController when click on button in UIWebView swift
[英]Swift Store and Restore ViewController state on Button Click
我正在使用Xcode 8和Swift 2.3
我瀏覽了幾個網站,它們是有關狀態恢復方法的指南:
https://www.raywenderlich.com/117471/state-restoration-tutorial
但是我需要通過傳遞已標識的鍵來存儲和恢復按鈕單擊時視圖控制器的狀態。
我無法在情節提要中使用單個標識符,因為我需要保存同一ViewController的許多實例,因此需要為每個實例使用不同的標識符,基於傳遞的鍵標識符,它應僅還原同一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
}
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
}
}
但是我需要通過傳遞已標識的鍵來存儲和恢復按鈕單擊時視圖控制器的狀態。
不。 單擊按鈕無法完成此操作。 UIKit提供了僅當應用程序進入BG模式時才對數據進行編碼的編碼器。 最有可能無法手動調用這些方法。 State Preservation / Restoration不是為此設計的,或者至少UIKit暫時不允許這樣做。 您可能必須自己編寫自己的狀態恢復機制,例如UserDefaults
或寫入磁盤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.