Skip to content

Commit

Permalink
Adding checklists
Browse files Browse the repository at this point in the history
  • Loading branch information
mriddle committed May 2, 2016
1 parent 5bdc47d commit 6bd5e87
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 7 deletions.
58 changes: 57 additions & 1 deletion AllListsViewController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class AllListsViewController: UITableViewController {
class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate {

var lists = [
Checklist(name: "Birthdays"),
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}
4 changes: 4 additions & 0 deletions Checklists.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -30,6 +31,7 @@
A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ItemDetailViewController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AllListsViewController.swift; path = ../AllListsViewController.swift; sourceTree = "<group>"; };
A8961D551CD31108006AFAF4 /* Checklist.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Checklist.swift; sourceTree = "<group>"; };
A8FAB6971CD6CB75007E7222 /* ListDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListDetailViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
6 changes: 3 additions & 3 deletions Checklists/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,7 +16,6 @@
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
Expand Down
89 changes: 86 additions & 3 deletions Checklists/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
<outlet property="delegate" destination="i4d-ru-O2O" id="iOt-sU-b1P"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Checklists" id="JLT-i1-af1"/>
<navigationItem key="navigationItem" title="Checklists" id="JLT-i1-af1">
<barButtonItem key="rightBarButtonItem" systemItem="add" id="639-gQ-I0h">
<connections>
<segue destination="BKw-Zu-67k" kind="presentation" identifier="AddChecklist" id="8u7-i1-QIO"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<segue destination="0EI-kG-L1l" kind="show" identifier="ShowChecklist" id="PpX-bR-tnW"/>
</connections>
Expand Down Expand Up @@ -52,7 +58,7 @@
<label opaque="NO" userInteractionEnabled="NO" tag="1001" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pll-Dd-HIC">
<rect key="frame" x="9" y="6" width="13" height="27"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Bold" family="Helvetica Neue" pointSize="22"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -86,7 +92,7 @@
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="AMX-6J-qkM">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
<sections>
<tableViewSection id="C2n-iL-8dR">
<cells>
Expand Down Expand Up @@ -138,6 +144,83 @@
</objects>
<point key="canvasLocation" x="861" y="1634"/>
</scene>
<!--Add Checklist-->
<scene sceneID="jGd-w3-BqM">
<objects>
<tableViewController id="704-DC-qeA" customClass="ListDetailViewController" customModule="Checklists" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="ZiQ-X6-jKb">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
<sections>
<tableViewSection id="lkI-8j-vSj">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="WaW-Lz-0bP">
<rect key="frame" x="0.0" y="99" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="WaW-Lz-0bP" id="HXi-ke-zUh">
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Name of the List" textAlignment="natural" adjustsFontSizeToFit="NO" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="1Zm-C4-47M">
<rect key="frame" x="8" y="7" width="584" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences" returnKeyType="done"/>
<connections>
<action selector="done" destination="704-DC-qeA" eventType="editingDidEndOnExit" id="xds-p3-RA0"/>
<outlet property="delegate" destination="704-DC-qeA" id="zmI-GP-v43"/>
</connections>
</textField>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
<connections>
<outlet property="dataSource" destination="704-DC-qeA" id="ImH-19-wfA"/>
<outlet property="delegate" destination="704-DC-qeA" id="YQD-5U-fUL"/>
</connections>
</tableView>
<toolbarItems/>
<navigationItem key="navigationItem" title="Add Checklist" id="Y5j-65-FtM">
<barButtonItem key="leftBarButtonItem" systemItem="cancel" id="Vqc-sb-kGg">
<connections>
<action selector="cancel" destination="704-DC-qeA" id="cUN-Je-9af"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" enabled="NO" systemItem="done" id="4nq-G7-fzh">
<connections>
<action selector="done" destination="704-DC-qeA" id="fxJ-CL-sKH"/>
</connections>
</barButtonItem>
</navigationItem>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="doneBarButton" destination="4nq-G7-fzh" id="lP2-Wu-7Nt"/>
<outlet property="textField" destination="1Zm-C4-47M" id="2iZ-u3-kmd"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="fF0-qu-vds" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1595" y="1634"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="yoE-Pf-QTm">
<objects>
<navigationController storyboardIdentifier="ListDetailNavigationController" id="BKw-Zu-67k" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="iX6-Vu-kDW">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="704-DC-qeA" kind="relationship" relationship="rootViewController" id="2LL-er-3us"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="CSt-Mt-FRW" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1595" y="923"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="0zI-Zw-Czb">
<objects>
Expand Down
59 changes: 59 additions & 0 deletions Checklists/ListDetailViewController.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 6bd5e87

Please sign in to comment.