From 6bd5e877fdf50958de1610671af4687c20e07465 Mon Sep 17 00:00:00 2001 From: mriddle Date: Mon, 2 May 2016 11:11:41 +1000 Subject: [PATCH] Adding checklists --- AllListsViewController.swift | 58 +++++++++++- Checklists.xcodeproj/project.pbxproj | 4 + Checklists/Base.lproj/LaunchScreen.storyboard | 6 +- Checklists/Base.lproj/Main.storyboard | 89 ++++++++++++++++++- Checklists/ListDetailViewController.swift | 59 ++++++++++++ 5 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 Checklists/ListDetailViewController.swift diff --git a/AllListsViewController.swift b/AllListsViewController.swift index 38f76b6..d8bf8a9 100644 --- a/AllListsViewController.swift +++ b/AllListsViewController.swift @@ -1,6 +1,6 @@ import UIKit -class AllListsViewController: UITableViewController { +class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate { var lists = [ Checklist(name: "Birthdays"), @@ -36,6 +36,19 @@ class AllListsViewController: UITableViewController { performSegueWithIdentifier("ShowChecklist", sender: checklist) } + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + if segue.identifier == "ShowChecklist" { + let controller = segue.destinationViewController as! ChecklistViewController + controller.checklist = sender as! Checklist + } else if segue.identifier == "AddChecklist" { + let navigationController = segue.destinationViewController as! UINavigationController + let controller = navigationController.topViewController as! ListDetailViewController + + controller.delegate = self + controller.checklistToEdit = nil + } + } + func cellForTableView(tableView: UITableView) -> UITableViewCell { let cellIdentifier = "Cell" if let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) { @@ -44,4 +57,47 @@ class AllListsViewController: UITableViewController { return UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier) } } + + func listDetailViewControllerDidCancel(controller: ListDetailViewController) { + dismissViewControllerAnimated(true, completion: nil) + } + + func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist) { + let newRowIndex = lists.count + + lists.append(checklist) + + let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0) + tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) + + dismissViewControllerAnimated(true, completion: nil) + } + + func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist) { + if let index = lists.indexOf(checklist) { + let indexPath = NSIndexPath(forRow: index, inSection: 0) + if let cell = tableView.cellForRowAtIndexPath(indexPath) { + cell.textLabel!.text = checklist.name + } + } + dismissViewControllerAnimated(true, completion: nil) + } + + override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { + lists.removeAtIndex(indexPath.row) + + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) + } + + override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { + let navigationController = storyboard!.instantiateViewControllerWithIdentifier("ListDetailNavigationController") as! UINavigationController + let controller = navigationController.topViewController as! ListDetailViewController + + controller.delegate = self + + let checklist = lists[indexPath.row] + controller.checklistToEdit = checklist + + presentViewController(navigationController, animated: true, completion: nil) + } } diff --git a/Checklists.xcodeproj/project.pbxproj b/Checklists.xcodeproj/project.pbxproj index ee46302..e4871ce 100644 --- a/Checklists.xcodeproj/project.pbxproj +++ b/Checklists.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */; }; A8961D541CD309B0006AFAF4 /* AllListsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */; }; A8961D561CD31108006AFAF4 /* Checklist.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D551CD31108006AFAF4 /* Checklist.swift */; }; + A8FAB6981CD6CB75007E7222 /* ListDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8FAB6971CD6CB75007E7222 /* ListDetailViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ItemDetailViewController.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AllListsViewController.swift; path = ../AllListsViewController.swift; sourceTree = ""; }; A8961D551CD31108006AFAF4 /* Checklist.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Checklist.swift; sourceTree = ""; }; + A8FAB6971CD6CB75007E7222 /* ListDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListDetailViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -69,6 +71,7 @@ A81172CB1CD0660F00BA3196 /* Assets.xcassets */, A81172CD1CD0660F00BA3196 /* LaunchScreen.storyboard */, A81172D01CD0660F00BA3196 /* Info.plist */, + A8FAB6971CD6CB75007E7222 /* ListDetailViewController.swift */, A81172D81CD0859800BA3196 /* ChecklistItem.swift */, A8961D551CD31108006AFAF4 /* Checklist.swift */, A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */, @@ -149,6 +152,7 @@ buildActionMask = 2147483647; files = ( A81172C71CD0660E00BA3196 /* ChecklistViewController.swift in Sources */, + A8FAB6981CD6CB75007E7222 /* ListDetailViewController.swift in Sources */, A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */, A8961D561CD31108006AFAF4 /* Checklist.swift in Sources */, A81172D91CD0859800BA3196 /* ChecklistItem.swift in Sources */, diff --git a/Checklists/Base.lproj/LaunchScreen.storyboard b/Checklists/Base.lproj/LaunchScreen.storyboard index 2e721e1..1fa91aa 100644 --- a/Checklists/Base.lproj/LaunchScreen.storyboard +++ b/Checklists/Base.lproj/LaunchScreen.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -15,7 +16,6 @@ - diff --git a/Checklists/Base.lproj/Main.storyboard b/Checklists/Base.lproj/Main.storyboard index 3879634..c7c8911 100644 --- a/Checklists/Base.lproj/Main.storyboard +++ b/Checklists/Base.lproj/Main.storyboard @@ -18,7 +18,13 @@ - + + + + + + + @@ -52,7 +58,7 @@ @@ -86,7 +92,7 @@ - + @@ -138,6 +144,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Checklists/ListDetailViewController.swift b/Checklists/ListDetailViewController.swift new file mode 100644 index 0000000..46d18d1 --- /dev/null +++ b/Checklists/ListDetailViewController.swift @@ -0,0 +1,59 @@ +import UIKit + +protocol ListDetailViewControllerDelegate: class { + func listDetailViewControllerDidCancel(controller: ListDetailViewController) + + func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist) + func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist) +} + +class ListDetailViewController: UITableViewController, UITextFieldDelegate { + + @IBOutlet weak var textField: UITextField! + @IBOutlet weak var doneBarButton: UIBarButtonItem! + + weak var delegate: ListDetailViewControllerDelegate? + + var checklistToEdit: Checklist? + + override func viewDidLoad() { + super.viewDidLoad() + + if let checklist = checklistToEdit { + title = "Edit Checklist" + textField.text = checklist.name + doneBarButton.enabled = true + } + } + + override func viewWillAppear(animated: Bool) { + super.viewWillAppear(animated) + textField.becomeFirstResponder() + } + + @IBAction func cancel() { + delegate?.listDetailViewControllerDidCancel(self) + } + + @IBAction func done() { + if let checklist = checklistToEdit { + checklist.name = textField.text! + delegate?.listDetailViewController(self, didFinishEditingChecklist: checklist) + } else { + let checklist = Checklist(name: textField.text!) + delegate?.listDetailViewController(self, didFinishAddingChecklist: checklist) + } + } + + override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + return nil + } + + func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { + let oldText: NSString = textField.text! + let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string) + doneBarButton.enabled = (newText.length > 0) + return true + } +} +