forked from lifuzu/TaskManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskDetailViewController.swift
77 lines (58 loc) · 1.77 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
//
// 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 {
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBOutlet var txtDesc: UITextField!
var task: Tasks? = nil
override func viewDidLoad() {
super.viewDidLoad()
if task != nil {
txtDesc.text = task?.desc
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK:- IBActions
@IBAction func done(sender: AnyObject) {
if task != nil {
editTask()
} else {
createTask()
}
dismissViewController()
}
@IBAction func cancel(sender: AnyObject) {
dismissViewController()
}
// MARK:- Dismiss ViewControllers
func dismissViewController() {
navigationController?.popViewControllerAnimated(true)
}
// MARK:- Create task
func createTask() {
let entityDescripition = NSEntityDescription.entityForName("Tasks", inManagedObjectContext: managedObjectContext)
let task = Tasks(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
task.desc = txtDesc.text!
do {
try managedObjectContext.save()
} catch _ {
}
}
// MARK:- Edit task
func editTask() {
task?.desc = txtDesc.text!
do {
try managedObjectContext.save()
} catch _ {
}
}
}