恢复 segued iOS UIViewControllers 的更好方法

Posted

技术标签:

【中文标题】恢复 segued iOS UIViewControllers 的更好方法【英文标题】:A better way to restore segued iOS UIViewControllers 【发布时间】:2015-12-02 11:30:04 【问题描述】:

我有一个显示选项列表的 UITableViewController。点击每个选项会使用 segue 导航到另一个视图控制器

需要以下代码来恢复应用的状态。但是有更清洁的方法吗?我在列表中包含了处理两个选项的代码,但图像有 10 个选项!

class SettingsViewController:UITableViewController 

var isRestoration = false
var aboutVC:AboutViewController?
var feedbackVC:FeedbackViewController?

override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(animated)
    if isRestoration 
        isRestoration = false
        restoreChildIfPresent()
     else 
        // nullify so when child is closed, we don't reopen
        // on next restoration
        forgetChildren()
    


override func restoreChildIfPresent() 
    if let aboutVC = aboutVC 
        self.navigationController?.pushViewController(aboutVC, animated:false)
     else if let feedbackVC = feedbackVC 
        self.navigationController?.pushViewController(feedbackVC, animated:false)
    


func forgetChildren() 
    aboutVC = nil
    charterVC = nil
    feedbackVC = nil


override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) 
    aboutVC = nil
    feedbackVC = nil
    if segue.identifier == "aboutSegue" 
        aboutVC = segue.destinationViewController as? AboutViewController    
     else if segue.identifier == "feedbackSegue" 
        feedbackVC = segue.destinationViewController as? FeedbackViewController
    


    override func decodeRestorableStateWithCoder(coder:NSCoder) 
    isRestoration = true
    aboutVC = coder.decodeObjectForKey("aboutVC") as? AboutUsViewController
    feedbackVC = coder.decodeObjectForKey("feedbackVC") as? FeedbackViewController
    super.decodeRestorableStateWithCoder(coder)


override func encodeRestorableStateWithCoder(coder:NSCoder) 
    coder.encodeObject(aboutVC, forKey:"aboutVC")
    coder.encodeObject(feedbackVC, forKey:"feedbackVC")
    super.encodeRestorableStateWithCoder(coder)

我很惊讶我需要 encodeObject() 子 VC,因为它们没有嵌入(对于那些嵌入 UITabbarController 的子 VC 确实需要 encodeObject() 子)。

我的主要“不喜欢”是 restoreChildIfPresent(),随着更多 ViewControllers 的添加和对 forgetChildren() 的丑陋需求的增加,它会增长

【问题讨论】:

【参考方案1】:

通过使用对 UIViewController 的单个引用,我已将问题中的代码简化为更易于管理的代码。但是结构是一样的。可以添加进一步的 Swifty-ness,但我对替代方法更感兴趣,或者确认这种方法是推荐的做法。

class SettingsViewController:UITableViewController 

var isRestoration = false
var childVC:UIViewController?

override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(animated)
    if isRestoration 
        isRestoration = false
        restoreChildIfPresent()
     else 
        // nullify so when child is closed, we don't reopen
        // on next restoration
        forgetChild()
    


override func restoreChildIfPresent() 
    if let childVC = childVC 
        self.navigationController?.pushViewController(childVC, animated:false)
    


func forgetChild() 
    childVC = nil


override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) 
    childVC = nil
    if segue.identifier == "aboutSegue" 
        childVC = segue.destinationViewController    
     else if segue.identifier == "feedbackSegue" 
        childVC = segue.destinationViewController
    


override func decodeRestorableStateWithCoder(coder:NSCoder) 
    isRestoration = true
    childVC = coder.decodeObjectForKey("childVC")
    super.decodeRestorableStateWithCoder(coder)


override func encodeRestorableStateWithCoder(coder:NSCoder) 
    coder.encodeObject(childVC, forKey:"childVC")
    super.encodeRestorableStateWithCoder(coder)

【讨论】:

以上是关于恢复 segued iOS UIViewControllers 的更好方法的主要内容,如果未能解决你的问题,请参考以下文章

无动画推送 segue

performSegueWithIdentifier:sender: 找不到我的转场

iOS从推送segue中检测unwind segue

嵌入 Segue - iOS 5

iOS:检测触地、segue、触地

IOS/Objective-C:导航栏自定义Segue效果对比Show Segue