-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThirdViewController.swift
124 lines (99 loc) · 5.1 KB
/
ThirdViewController.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
112
113
114
115
116
117
118
119
120
121
122
//
// ThirdViewController.swift
// hw#7
//
// Created by Gering Dong on 1/10/19.
// Copyright © 2019 Gering Dong. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class ThirdViewController: UIViewController {
var key = ""
var id: String = ""
var newTitle: String = ""
var newDescription: String = ""
var check = 0
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var lblDescription: UILabel!
@IBOutlet weak var txtFieldTitle: UITextField!
@IBOutlet weak var txtRecipeDetails: UITextView!
var labelTitle = "Title"
var recipeDiscription = "Description"
var TitleIntxtFeild = ""
var recipeDetails = ""
override func viewDidLoad() {
super.viewDidLoad()
self.lblTitle?.text = labelTitle
self.lblDescription?.text = recipeDiscription
self.txtFieldTitle?.text = TitleIntxtFeild
self.txtRecipeDetails?.text = recipeDetails
}
@IBAction func cancelBtn(_ sender: Any) {
let alert = UIAlertController(title: "Cancel", message: "Are you sure you want to discard these changes?", preferredStyle: .alert)
let Yes = UIAlertAction(title: "Yes", style: .default){(action) in
self.dismiss(animated: true, completion: nil)
}
let No = UIAlertAction(title:"No", style: .default, handler: nil)
alert.addAction(Yes)
alert.addAction(No)
present(alert, animated: true)
}
@IBAction func saveBtn(_ sender: Any) {
//var title: String = self.txtFieldTitle.text!
//var description: String = self.txtRecipeDetails.text!
let title = self.txtFieldTitle.text!
let decription = self.txtRecipeDetails.text!
if check == 0 {
if title == "" || decription == ""{
let alert = UIAlertController(title: "Oops!", message: "The Title or the Description is empty!", preferredStyle: .alert)
let Ok = UIAlertAction(title: "Ok", style: .default, handler:nil)
alert.addAction(Ok)
self.present(alert, animated: true)
}
else{
let alert = UIAlertController(title: "Save", message: "would you like to save ?", preferredStyle: .alert)
let yes = UIAlertAction(title: "Yes", style: .default){(action) in
let parameter = ["key": self.key, "title": self.txtFieldTitle.text!, "description": self.txtRecipeDetails.text!] as [String:Any]
let link = "http://www.tageninformatics.com/client/jwu/csis3070_recipe/"
AF.request(link + "create", method: .get, parameters: parameter).responseJSON{
response in
self.performSegue(withIdentifier: "thirdToFirst", sender: self)
}
}
let cancel = UIAlertAction (title: "Cancel", style: .default, handler: nil)
alert.addAction(yes)
alert.addAction(cancel)
self.present(alert, animated: true)
}
}
if check == 1 {
if title == "" || decription == "" {
let alert = UIAlertController(title: "Oops!", message: "The Title or the Description is empty!", preferredStyle: .alert)
let Ok = UIAlertAction(title: "Ok", style: .default, handler:nil)
alert.addAction(Ok)
self.present(alert, animated: true)
}
else{
let alert = UIAlertController(title: "Save", message: "would you like to save ?", preferredStyle: .alert)
let save = UIAlertAction(title: "save", style: .default){(action) in
let parameters = ["key": self.key, "id": self.id] as [String: Any]
print(self.id)
let link = "http://www.tageninformatics.com/client/jwu/csis3070_recipe/"
AF.request(link + "delete", method: .get, parameters: parameters).responseJSON{
response in
let parameter = ["key": self.key, "title": self.txtFieldTitle.text!, "description": self.txtRecipeDetails.text!] as [String: Any]
AF.request(link + "create", method: .get, parameters: parameter).responseJSON{
response in
self.performSegue(withIdentifier: "thirdToFirst", sender: self)
}
}
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(save)
alert.addAction(cancel)
self.present(alert, animated: true)
}
}
}
}