Skip to content

Instantly share code, notes, and snippets.

@ndbroadbent
Created July 12, 2014 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndbroadbent/b4a5d43749333485b32f to your computer and use it in GitHub Desktop.
Save ndbroadbent/b4a5d43749333485b32f to your computer and use it in GitHub Desktop.
Swift Hackathon Jul 2014
//
// MasterViewController.swift
// SwiftClassTodo
//
// Created by JP Simard on 7/12/14.
// Copyright (c) 2014 Realm. All rights reserved.
//
import UIKit
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var objects = [Task]()
override func awakeFromNib() {
super.awakeFromNib()
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
self.clearsSelectionOnViewWillAppear = false
self.preferredContentSize = CGSize(width: 320.0, height: 600.0)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
self.navigationItem.rightBarButtonItem = addButton
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func insertNewObject(sender: AnyObject) {
let alert = UIAlertController(title: "New Task",
message: "",
preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler { textField in
textField.placeholder = "Buy milk"
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { action in
alert.dismissViewControllerAnimated(true) {}
}
alert.addAction(cancelAction)
let createAction = UIAlertAction(title: "Create",
style: .Default) { action in
let textField = alert.textFields[0] as UITextField
self.addTask(Task(name: textField.text))
}
alert.addAction(createAction)
presentViewController(alert, animated: true) {}
}
func addTask(task: Task) {
objects += task
let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
// #pragma mark - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = objects[indexPath.row]
(segue.destinationViewController as DetailViewController).detailItem = object
}
}
// #pragma mark - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = objects[indexPath.row]
cell.textLabel.text = object.description
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
objects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
let object = objects[indexPath.row]
self.detailViewController!.detailItem = object
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment