-
Notifications
You must be signed in to change notification settings - Fork 10
/
TaskDetailViewController.swift
executable file
·111 lines (92 loc) · 3.58 KB
/
TaskDetailViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// TaskDetailViewController.swift
// TaskManager
//
// Created by Ravi Shankar on 12/07/14.
// Copyright (c) 2014 Ravi Shankar. All rights reserved.
//
import UIKit
import CoreData
class TaskDetailViewController: UIViewController, UITextFieldDelegate {
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
//outlets for the text fields
@IBOutlet var txtDesc: UITextField!
//@IBOutlet var txtDesc2: UITextField!
@IBOutlet var txtDesc2: UITextView!
//instances of Tasks entity
var task: Tasks? = nil
var task2: Tasks? = nil
//get the right text in the boxes
override func viewDidLoad() {
//call the superview
super.viewDidLoad()
txtDesc.delegate = self
//txtDesc2.delegate = self
// if there's something in the task text from the object store
if task != nil{
//set the text in the text box to be this text
txtDesc.text = (task as Tasks!).desc //added the !
}
else{
txtDesc.placeholder = "Name"
//set placeholder text
}
// if there's something in the task2 text from the object store
if task2 != nil{
//set the text in the second text box to be this text
txtDesc2.text = (task2 as Tasks!).desc2
}
else{
// txtDesc2.placeholder = "ph2"
//set placeholder text
}
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
txtDesc.resignFirstResponder()
return true
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true)
}
//when done (or submmit) button is pressed
@IBAction func done(sender: AnyObject) {
//if the user presses done and there's something in either box right off the bat, call editTask()
if task != nil && task2 != nil{
editTask()
} else {
//but if there's not something in either box right off the bat, call createTask()
createTask()
}
dismissViewController()
}
@IBAction func cancel(sender: AnyObject) {
dismissViewController()
}
func dismissViewController() {
navigationController.popViewControllerAnimated(true)
}
//basically, create means you need to allocate a space in the object store for this object, then save it there
func createTask() {
//grab the entity description
let entityDescription = NSEntityDescription.entityForName("Tasks", inManagedObjectContext: managedObjectContext)
//assign task variable to hav a place in the object store
let task = Tasks(entity: entityDescription, insertIntoManagedObjectContext: managedObjectContext)
//set desc in the store to be the user's text from txtDesc text box
task.desc = txtDesc.text
//set desc2 in the store to be the user's text from txtDesc2 text box
task.desc2 = txtDesc2.text
//save the new Tasks object
managedObjectContext.save(nil)
}
func editTask() {
//figure out where the object is in the data store, and edit it
(task as Tasks!).desc = txtDesc.text //added the !
(task2 as Tasks!).desc2 = txtDesc2.text
//save the edited Tasks object
managedObjectContext.save(nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}