特殊的选取器视图/功能/行动问题
所以今天我一直在研究一个温度转换器应用程序,以便将我自己介绍到我迄今在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 = ""
}
}
以上是 特殊的选取器视图/功能/行动问题 的全部内容, 来源链接: utcz.com/qa/257672.html