From 3ede4b173a2baa4f608e44b7be45ac21e29cff50 Mon Sep 17 00:00:00 2001 From: mriddle Date: Mon, 2 May 2016 12:38:56 +1000 Subject: [PATCH] Making use of NSUserDefaults - Remember Checklist on app close / suspension --- AllListsViewController.swift | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/AllListsViewController.swift b/AllListsViewController.swift index a500009..0dc5acd 100644 --- a/AllListsViewController.swift +++ b/AllListsViewController.swift @@ -1,12 +1,25 @@ import UIKit -class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate { +class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate, UINavigationControllerDelegate { var dataModel: DataModel! override func viewDidLoad() { super.viewDidLoad() } + + override func viewDidAppear(animated: Bool) { + super.viewDidAppear(animated) + + navigationController?.delegate = self + + let index = NSUserDefaults.standardUserDefaults().integerForKey("ChecklistIndex") + + if index != -1 { + let checklist = dataModel.lists[index] + performSegueWithIdentifier("ShowChecklist", sender: checklist) + } + } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() @@ -27,6 +40,8 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + NSUserDefaults.standardUserDefaults().setInteger(indexPath.row, forKey: "ChecklistIndex") + let checklist = dataModel.lists[indexPath.row] performSegueWithIdentifier("ShowChecklist", sender: checklist) } @@ -95,4 +110,11 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel presentViewController(navigationController, animated: true, completion: nil) } + + func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) { + + if viewController === self { + NSUserDefaults.standardUserDefaults().setInteger(-1, forKey: "ChecklistIndex") + } + } }