-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultViewController.swift
More file actions
113 lines (88 loc) · 3.47 KB
/
Copy pathResultViewController.swift
File metadata and controls
113 lines (88 loc) · 3.47 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
//
// ResultViewController.swift
// SimpleCalculator
//
// Created by framgia on 10/25/16.
// Copyright © 2016 framgia. All rights reserved.
//
import UIKit
protocol ResultViewControllerDelegate {
func onDataSaved(data: [Result])
}
class ResultViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// MARK: - Properties
@IBOutlet weak var valueLabel: UILabel!
@IBOutlet weak var intLabel: UILabel!
@IBOutlet weak var stringLabel: UILabel!
@IBOutlet weak var floatLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
var result = Result(argument1: "", argument2: "", operation: "", result: "", dataType: "")
var results = [Result]()
var numberOfInt = 0
var numberOfFloat = 0
var numberOfString = 0
var delegate: ResultViewControllerDelegate?
var onClosureButtonTapped: ((data: [Result]) -> Void)? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.reloadData()
loadData()
initViews()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadData() {
results.append(result!)
if let savedResults = Result.loadResults() {
results += savedResults
}
for r: Result in results {
if (r.dataType == DataTypes.int) {
numberOfInt += 1
} else if (r.dataType == DataTypes.float){
numberOfFloat += 1
} else {
numberOfString += 1
}
}
}
func initViews() {
valueLabel.text = result?.result
intLabel.text = intLabel.text! + "\(numberOfInt)"
stringLabel.text = stringLabel.text! + "\(numberOfString)"
floatLabel.text = floatLabel.text! + "\(numberOfFloat)"
}
// MARK: - Actions
@IBAction func delegate(sender: UIButton) {
Result.saveResults(results)
delegate?.onDataSaved(results)
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func closure(sender: UIButton) {
Result.saveResults(results)
if let onClosureButtonTapped = self.onClosureButtonTapped {
onClosureButtonTapped(data: results)
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
@IBAction func postNotification(sender: UIButton) {
Result.saveResults(results)
NSNotificationCenter.defaultCenter()
.postNotificationName(NotificationKeys.notificationKey,
object: nil,
userInfo: ["message": results, "date": NSDate()])
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - TableView delegate
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return results.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("LabelCell", forIndexPath: indexPath)
cell.textLabel?.text = results[indexPath.row].result
return cell
}
}