-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddItemViewController.swift
More file actions
88 lines (62 loc) · 2.59 KB
/
addItemViewController.swift
File metadata and controls
88 lines (62 loc) · 2.59 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
//
// addItemViewController.swift
// CoreDataExample
//
// Created by Farhan Syed on 4/16/17.
// Copyright © 2017 Farhan Syed. All rights reserved.
//
import UIKit
import CoreData
class addItemViewController: UIViewController, UITextViewDelegate {
@IBOutlet var itemEntryTextView: UITextView?
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func saveContact(_ sender: Any) {
if (itemEntryTextView?.text.isEmpty)! || itemEntryTextView?.text == "Type anything..."{
print("No Data")
let alert = UIAlertController(title: "Please Type Something", message: "Your entry was left blank.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default) { action in
})
self.present(alert, animated: true, completion: nil)
} else {
guard let entryText = itemEntryTextView?.text else {
return
}
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/YY"
let currentDate = formatter.string(from: date)
let timeFormatter = DateFormatter()
timeFormatter.timeStyle = .short
let currentTime = timeFormatter.string(from: date)
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newEntry = Item(context: context)
newEntry.name = entryText
newEntry.date = currentDate
newEntry.time = currentTime
(UIApplication.shared.delegate as! AppDelegate).saveContext()
dismiss(animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
itemEntryTextView?.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.text = ""
textView.textColor = UIColor.black
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}