【发布时间】:2017-12-21 23:07:00
【问题描述】:
我正在开发一个 ios 应用程序,其中有一个按钮,每次单击时都需要运行大量计算,但按钮操作仅在第一次时有效。谁能帮我解决这个问题?
这是按钮的代码:
@IBAction func Calculate_btn(_ sender: UIButton) {
s2 = 0.0
mintext.text = ""
maxtext.text = ""
avtext.text = ""
if speedUser.text == "" {
let alert = UIAlertController(title: "Error", message: "Give the speed to calculate the new temperature !", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else if (Int(speedUser.text!)!<0) || (Int(speedUser.text!)!>20) {
let alert = UIAlertController(title: "Error", message: "Give a valid speed : between 0 and 20 Km/h!", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else {
refTot = ref + ref2
rcfTot = rcf + rcf2
airPermTot = airPermFab + airPermFab2
print("refTot",refTot)
print("rcfTot",rcfTot)
print("airPermTot",airPermTot)
var s2 = (speedUser.text! as NSString).floatValue
Calcul(s: s2, rc: refTot, re: rcfTot, air: airPermTot)
s2 = 0
}
}
【问题讨论】:
-
Calcul在做什么?它是否执行任何 UI 任务? -
Calcul 进行了大量的物理计算并将结果放入图表和 4 个标签中@Mukesh
-
您是否使用断点来证明当您触摸按钮时该方法只运行一次(非常不可能)?当您使用调试器单步执行代码时,您的逻辑是否正确?
-
@RoboticCat 是的,我什么都试过了,按钮上的动作只在第一次起作用
标签: ios swift xcode button action