特殊的选择器视图/功能/操作问题



所以今天我一直在开发一个温度转换器应用程序,以重新介绍我迄今为止在 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 = ""
    }
}

你的错误是你的pickerView:DidSelectRow:inComponent:在你的convertTemperatureButton:函数内。 它必须是viewController中的顶级函数才能调用它。

pickerView:DidSelectRow:inComponent:移动到顶层,让它在viewController中设置属性conversionType,然后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 = ""
    }
}

相关内容

  • 没有找到相关文章

最新更新