-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreationViewController.swift
executable file
·89 lines (75 loc) · 3.18 KB
/
CreationViewController.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
//
// CreationViewController.swift
// ThreeByFive
//
// Created by porcupal on 10/27/18.
// Copyright © 2018 porcupal. All rights reserved.
//
import UIKit
class CreationViewController: UIViewController {
var flashcardsController: ViewController!
@IBOutlet weak var questionTextField: UITextField!
@IBOutlet weak var answerTextField: UITextField!
@IBOutlet weak var optOneTextField: UITextField!
@IBOutlet weak var optTwoTextField: UITextField!
@IBOutlet weak var optThreeTextField: UITextField!
var initialQuestion: String?
var initialAnswer: String?
var initialOptOne: String?
var initialOptTwo: String?
var initialOptThree: String?
override func viewDidLoad() {
super.viewDidLoad()
questionTextField.text = initialQuestion
answerTextField.text = initialAnswer
optOneTextField.text = initialOptOne
optTwoTextField.text = initialOptTwo
optThreeTextField.text = initialOptThree
// Do any additional setup after loading the view.
}
@IBAction func didTapOnCancel(_ sender: Any) {
dismiss(animated: true)
}
@IBAction func didTapOnDone(_ sender: Any) {
let questionText = questionTextField.text
let answerText = answerTextField.text
let optOneText = optOneTextField.text
let optTwoText = optTwoTextField.text
let optThreeText = optThreeTextField.text
// lastly, check if an existing Flashcard is being edited
let isExisting = (initialQuestion != nil) ? true: false
// add alert window and 'Ok' option
let alert = UIAlertController(title: "Missing Text", message: "You need both a question and an answer", preferredStyle: .alert)
let optAlert = UIAlertController(title: "Missing Multiple Choice", message: "Insert options to proceed", preferredStyle: .alert)
// add 'Ok' option to alert window
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
optAlert.addAction(okAction)
// check valid text fields before updating flashcard
if (questionText == nil || answerText == nil || questionText!.isEmpty || answerText!.isEmpty) {
present(alert, animated: true)
}
if (optOneText == nil || optTwoText == nil || optThreeText == nil ||
optOneText!.isEmpty || optTwoText!.isEmpty || optThreeText!.isEmpty) {
present(optAlert, animated:true)
} else {
flashcardsController.updateFlashcard(
question: questionText!,
answer: answerText!,
optOne: optOneText!,
optTwo: optTwoText!,
optThree: optThreeText!,
isExisting: isExisting
);
dismiss(animated: true)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}