所以今天我一直在研究一个温度转换器应用程序,重新介绍我迄今为止在swift中学到的概念(我目前正在上网课程)我决定添加一个选择器视图。我没有任何错误,但是当我按下UIButton时,它不响应我的代码中列出的任何if语句 - 这是所有温度转换的地方。所以我的问题是,为什么我的按钮在按下时没有转换在文本字段中输入的温度,我该如何解决?谢谢
代码:
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 :(得分:0)
您的错误是pickerView:DidSelectRow:inComponent:
位于convertTemperatureButton:
函数内部。它必须是viewController
中的顶级函数才能被调用。
将pickerView:DidSelectRow:inComponent:
移到顶层,让它在conversionType
中设置属性viewController
,然后让convertTemperature
使用该属性:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var conversionType = ""
override func viewDidLoad() {
super.viewDidLoad()
conversionType = temperatureConversions[0]
}
// 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]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
conversionType = temperatureConversions[row]
}
@IBOutlet weak var newTemperatureLabel: UILabel!
// Converts number entered from textfield into double.
@IBAction func convertTemperatureButton(sender: UIButton) {
var temperatureTextFieldConversion = temperatureTextField.text
var temperatureTextFieldToDouble = Double((temperatureTextFieldConversion as NSString).doubleValue)
if conversionType == "°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
}
if conversionType == "°Celcius-°Farenheit"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble * 1.8 + 32)" + " °Farenheit"
}
if conversionType == "°Farenheit-Kelvin"{
newTemperatureLabel.text = "\(((temperatureTextFieldToDouble - 32) / 1.8) + 273.15)" + " Kelvin"
}
if conversionType == "Kelvin-°Farenheit"{
newTemperatureLabel.text = "\((temperatureTextFieldToDouble - 273.15) * 1.8 + 32.00)" + " °Farenheit"
}
if conversionType == "°Celcius-Kelvin"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble + 273.15)" + " Kelvin"
}
if conversionType == "Kelvin-°Celcius"{
newTemperatureLabel.text = "\(temperatureTextFieldToDouble - 273.15)" + " °Celcius"
}
newTemperatureLabel.hidden = false
temperatureTextField.resignFirstResponder()
temperatureTextField.text = ""
}
}