-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperatureConverterViewController.swift
93 lines (82 loc) · 3.91 KB
/
TemperatureConverterViewController.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
//
// TemperatureConverterViewController.swift
// UCISwiftConverter
//
// Created by Adam Cherochak on 2015/01/13.
// Copyright (c) 2015 Adam Cherochak. All rights reserved.
// Added to homework project 2015-04-08 Adam Cherochak
// 2015-04-20-1102 wk3
//
import UIKit
class TemperatureConverterViewController: UIViewController, UITextFieldDelegate {
//2.i
@IBOutlet weak var fahrenheitTextField: UITextField!
@IBOutlet weak var celsiusTextField: UITextField!
// //wk2 Challenge 1 & 2: call graph xib from temperature xib
// @IBAction func btnShowGraph(sender: AnyObject) {
// let graphViewController = FCCurvesViewController(nibName: "FCCurves", bundle: nil)
// self.navigationController?.pushViewController(graphViewController, animated: false)
// }
//wk3-3.c
var conversions: Array<AnyObject> = []
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//wk3-3.e
func favoriteButtonPressed(sender: AnyObject) {
// save the favorite instance here get the last favorite we prepped and store it
if let data: NSData = NSUserDefaults.standardUserDefaults().objectForKey("FavoriteConversions") as? NSData {
var savedFavorites: Array<AnyObject> = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Array
if conversions.count > 0
{
savedFavorites.append(conversions.last!)
let updatedFavorites = NSKeyedArchiver.archivedDataWithRootObject(savedFavorites)
NSUserDefaults.standardUserDefaults().setValue(updatedFavorites, forKey: "FavoriteConversions")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//wk3-3.b create a favorite button
let favoriteBarButton = UIBarButtonItem(image: UIImage(named: "favoritesButton_60x60"), style:
UIBarButtonItemStyle.Plain, target: self, action: "favoriteButtonPressed:")
self.navigationItem.rightBarButtonItem = favoriteBarButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//wk3-3.d
func createConversionObject(inputValue: Double, inputUnits: String, outputValue: Double, outputUnits: String) {
let favoriteConversion = Favorite(conversionType: "Temperature", inputValue: inputValue, inputUnits: inputUnits, outputValue: outputValue, outputUnits: outputUnits)
conversions += [favoriteConversion]
}
func calculateCelsius(fahrenheitTemp: Double) -> String {
var celsiusTemp = (fahrenheitTemp - 32) * 5 / 9
self.createConversionObject(fahrenheitTemp, inputUnits: "ºF", outputValue: celsiusTemp, outputUnits: "ºC")
return "\(celsiusTemp)"
}
func calculateFahrenheit(celsiusTemp: Double) -> String{
var fahrenheitTemp = celsiusTemp * 9 / 5 + 32
self.createConversionObject(celsiusTemp, inputUnits: "ºC", outputValue: fahrenheitTemp, outputUnits: "ºF")
return "\(fahrenheitTemp)"
}
func textFieldShouldReturn(textField: UITextField) -> Bool{
if textField == fahrenheitTextField {
let fahrenheitTemp: Double = (fahrenheitTextField.text as NSString).doubleValue
celsiusTextField.text = calculateCelsius(fahrenheitTemp)
}
else
if textField == celsiusTextField {
let celsiusTemp: Double = (celsiusTextField.text as NSString).doubleValue
fahrenheitTextField.text = calculateFahrenheit(celsiusTemp)
}
textField.resignFirstResponder()
return true
}
}