所以这是我的代码,正如您所看到的,如果按钮的图像相同,我将尝试更改activitylevel
的值,这一切正常,它会更改值,但是当我尝试更改在CaloriesCalculator
活动的let tde = activity * bmr
函数中向下使用它仍然等于0.0
。我知道我可以用UserDefaults
保存它,但这对我来说似乎不是正确的方法。任何帮助将不胜感激。
class MenuController: UIViewController {
var calGoal: Double = 0.0
var activitylevel: Double = 0.0
@IBOutlet weak var Weight: UITextField!
@IBOutlet weak var Height: UITextField!
@IBOutlet weak var Age: UITextField!
@IBAction func ActivityLevelButton(_ sender: UIButton) {
if let ButtonImage = LightlyActive.image(for: .normal),
let Image = UIImage(named: "LightlyActiveGreen"),
ButtonImage.pngData() == Image.pngData()
{
activitylevel = 1.375
print(activitylevel)
}; if let ButtonImage = Active.image(for: .normal),
let Image = UIImage(named: "ActiveGreen"),
ButtonImage.pngData() == Image.pngData() {
activitylevel = 1.55
print(activitylevel)
}; if let ButtonImage = VeryActive.image(for: .normal),
let Image = UIImage(named: "VeryActiveGreen"),
ButtonImage.pngData() == Image.pngData() {
activitylevel = 1.755
print(activitylevel)
}
}
@IBAction func CaloriesCalculator(_ sender: UIButton) {
let w = Double(Weight.text!) ?? 0
let h = Double(Height.text!)! * 0.393701
let a = Double(Age.text!) ?? 0
let bmr = 66 + (6.23 * w) + (12.7 * h) - (6.8 * a)
let activity = activitylevel
let tde = activity * bmr
calGoal = tde
print(activitylevel)
}