gpt4 book ai didi

ios - 在Swift中如何获取监听分段控件的按钮?

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:26 26 4
gpt4 key购买 nike

我正在尝试构建一个温度转换器应用程序。我使用分段控件供用户选择应如何计算温度(摄氏度到华氏度以及从华氏度到摄氏度)。我还创建了一个按钮,用于转换分段控件中所选方法输入的温度。

这是我在 Controller 中创建的函数:

@IBAction func convertTemp(sender: AnyObject) {
let t = Double(tempTextfield.text!)
let type = converterType.selectedSegmentIndex
let tempM = tempModel(temp:t!)

if type == 0 {
finalTemp.text = String(tempM.celsius2Fahrenheit())
}

if type == 1 {
finalTemp.text = String(tempM.fahrenheit2Celsius())
}
}

这是我的模型中的内容。

class tempModel {
var temp: Double

init (temp:Double){
self.temp = temp
}
func celsius2Fahrenheit()->Double{
return 32 + temp * 5 / 9;
}
func fahrenheit2Celsius()->Double{
return (temp - 32) * 5/9;
}
}

我不确定我做错了什么。除了 Button(convert) 之外的所有内容都按照我希望的方式工作。我似乎找不到错误。

我不知道这是否有帮助,但我收到了这个错误:

2015-11-16 18:07:02.496 TemperatureConverer[5201:194432] Can't find keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad; using 4131139949_Portrait_iPhone-Simple-Pad_Default
2015-11-16 18:07:04.827 TemperatureConverer[5201:194432] Can't find keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad; using 4131139949_Portrait_iPhone-Simple-Pad_Default (lldb)

最佳答案

我找到了一个临时修复方法。我的问题是我的 viewController 和我的 tempModel 之间的通信。当我删除 tempModel 类并将该方法直接插入 Controller 中的函数时,我的代码起作用了。所以在我的 Controller 中,我有这个..

@IBAction func convertTemp(sender: AnyObject) {

let temp = Double(tempTextfield.text!)!
var newTemp = ""

if converterType.selectedSegmentIndex == 0{
newTemp = String(format: "%.2f Farenheit", 32+temp*5/9)
}
if converterType.selectedSegmentIndex == 1{
newTemp = String(format: "%.2f Celsius",(temp-32)*5/9)
}
finalTemp.text = newTemp
}

我知道最终我必须学习如何将我的 Controller 和模型链接在一起。但在我弄清楚之前,这是解决我的问题的一种解决方案。

关于ios - 在Swift中如何获取监听分段控件的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com