Skip to content

Commit

Permalink
Making use of NSUserDefaults - Remember Checklist on app close / susp…
Browse files Browse the repository at this point in the history
…ension
  • Loading branch information
mriddle committed May 2, 2016
1 parent efca2b0 commit 3ede4b1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion AllListsViewController.swift
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
}
}

0 comments on commit 3ede4b1

Please sign in to comment.