IPhone : The Peculiar Picker View/Function/Action Issue

on Sunday, April 19, 2015

so today I've been working on a temperature converter app to reintroduce myself to the concepts I had learned so far in swift (I'm currently in the process of taking a class online) and I decided to add in a picker view. I don't have any bugs, but when I press the UIButton it doesn't respond to any of the if statements listed in my code - which are where all the temperature conversions are. So my question is, why is my button when pressed not converting the temperature entered in the text field, and how do I fix it? Thanks


Code:



import UIKit

class ViewController: UIViewController, UIPickerViewDelegate {


override func viewDidLoad() {
super.viewDidLoad()


}

// Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var temperatureTextField: UITextField!

@IBOutlet weak var conversionTypePickerView: UIPickerView!

var temperatureConversions = ["°Farenheit-°Celcius", "°Celcius-°Farenheit", "°Farenheit-Kelvin", "Kelvin-°Farenheit", "°Celcius-Kelvin", "Kelvin-°Celcius"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return temperatureConversions.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return temperatureConversions[row]
}

@IBOutlet weak var newTemperatureLabel: UILabel!

@IBAction func convertTemperatureButton(sender: UIButton) {
var temperatureTextFieldConversion = temperatureTextField.text
var temperatureTextFieldToDouble = Double((temperatureTextFieldConversion as NSString).doubleValue)

// Converts number entered from textfield into double.

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){

if temperatureConversions[row] == "°Farenheit-°Celcius"{
newTemperatureLabel.text = "\((temperatureTextFieldToDouble - 32) / 1.8)" + " °Celcius"

//this is the first example of the if statements I mentioned above, other five are listed below

}
else{

}

if temperatureConversions[row] == "°Celcius-°Farenheit"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble * 1.8 + 32)" + " °Farenheit"


}
else{

}

if temperatureConversions[row] == "°Farenheit-Kelvin"{
newTemperatureLabel.text = "\(((temperatureTextFieldToDouble - 32) / 1.8) + 273.15)" + " Kelvin"

}
else{

}

if temperatureConversions[row] == "Kelvin-°Farenheit"{
newTemperatureLabel.text = "\((temperatureTextFieldToDouble - 273.15) * 1.8 + 32.00)" + " °Farenheit"

}
else {

}

if temperatureConversions[row] == "°Celcius-Kelvin"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble + 273.15)" + " Kelvin"

}
else{

}

if temperatureConversions[row] == "Kelvin-°Celcius"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble - 273.15)" + " °Celcius"

}
else{

}


}

newTemperatureLabel.hidden = false
temperatureTextField.resignFirstResponder()
temperatureTextField.text = ""
}


}


0 comments:

Post a Comment