Repository navigation
Expand file tree
/
Copy pathViewController.swift.txt
More file actions
143 lines (112 loc) · 4.12 KB
/
Copy pathViewController.swift.txt
File metadata and controls
143 lines (112 loc) · 4.12 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// ViewController.swift
// MyContacts
//
// Created by Charles Konkol on 2016-05-05
// Copyright © 2016 Chuck Konkol. All rights reserved.
//
import UIKit
//0) Add import for CoreData
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var fullname: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var btnSave: UIButton!
@IBOutlet weak var status: UILabel!
@IBAction func btnSave(sender: AnyObject) {
//1 Add Save Logic
//**Begin Copy**
if (contactdb != nil)
{
contactdb.setValue(fullname.text, forKey: "fullname")
contactdb.setValue(email.text, forKey: "email")
contactdb.setValue(phone.text, forKey: "phone")
}
else
{
let entityDescription =
NSEntityDescription.entityForName("Contact",inManagedObjectContext: managedObjectContext)
let contact = Contact(entity: entityDescription!,
insertIntoManagedObjectContext: managedObjectContext)
contact.fullname = fullname.text!
contact.email = email.text!
contact.phone = phone.text!
}
var error: NSError?
do {
try managedObjectContext.save()
} catch let error1 as NSError {
error = error1
}
if let err = error {
status.text = err.localizedFailureReason
} else {
self.dismissViewControllerAnimated(false, completion: nil)
}
//**End Copy**
}
@IBAction func btnBack(sender: AnyObject) {
//2) Dismiss ViewController
//**Begin Copy**
self.dismissViewControllerAnimated(false, completion: nil)
//**End Copy**
}
//3) Add ManagedObject Data Context
//**Begin Copy**
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
//**End Copy**
//4) Add variable contactdb (used from UITableView
//**Begin Copy**
var contactdb:NSManagedObject!
//**End Copy**
override func viewDidLoad() {
super.viewDidLoad()
//5 Add logic to load db. If contactdb has content that means a row was tapped on UiTableView
//**Begin Copy**
if (contactdb != nil)
{
fullname.text = contactdb.valueForKey("fullname") as? String
email.text = contactdb.valueForKey("email") as? String
phone.text = contactdb.valueForKey("phone") as? String
btnSave.setTitle("Update", forState: UIControlState.Normal)
}
fullname.becomeFirstResponder()
// Do any additional setup after loading the view.
//Looks for single or multiple taps
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.DismissKeyboard))
//Adds tap gesture to view
view.addGestureRecognizer(tap)
// Do any additional setup after loading the view, typically from a nib.
//**End Copy**
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//6 Add to hide keyboard
//**Begin Copy**
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches , withEvent:event)
if (touches.first as UITouch!) != nil {
DismissKeyboard()
}
}
//**End Copy**
//7 Add to hide keyboard
//**Begin Copy**
func DismissKeyboard(){
//forces resign first responder and hides keyboard
fullname.endEditing(true)
email.endEditing(true)
phone.endEditing(true)
}
//**End Copy**
//8 Add to hide keyboard
//**Begin Copy**
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true;
}
//**End Copy**
}