由于某种原因,当我单击计算按钮时,它不会在我创建的名为resultLabel的标签中显示我的结果。开关工作但不是按钮。我是编码的新手,本课的书是针对xCode 6的,我有最新版本的xCode。我检查了我的代码,但似乎无法找出问题,因为我没有错误。谢谢你的帮助。
import UIKit
class ViewController: UIViewController {
// properties for programmatically interacting with UI components
@IBOutlet weak var switchControl: UISwitch!
@IBOutlet weak var switchLabel: UILabel!
@IBOutlet weak var weightTextField: UITextField!
@IBOutlet weak var heightTextField: UITextField!
@IBOutlet weak var resultLabel: UILabel!
var w:Float = 0;
var h:Float = 0;
var bmi:Float = 0;
override func viewDidLoad() {
super.viewDidLoad()
// select weightTextField so keypad displays when the view loads
weightTextField.becomeFirstResponder()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func changeSwitchLabel(_ sender: UISwitch) {
if (sender.isOn == true) {
switchLabel.text = "Eng"
}
else{
switchLabel.text = "Met"
}
}
@IBAction func calculateBMI(_ sender: Any) {
if switchControl.isOn == true
{
let strW:NSString = weightTextField.text! as String as NSString
w = strW.floatValue
let strH:NSString = heightTextField.text! as String as NSString
h = strH.floatValue
bmi = (w * 702) / (h * h)
var str = ""
switch (bmi)
{
case 0...18.5:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Underweight") as String
case 18.5...24.9:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Normal") as String
case 25...29.9:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Overweight" ) as String
default:
str = NSString(format: "Your BMI is %2.f ", bmi, ". You are Obese") as String
}
resultLabel.text = str
}
else
{
let strW:NSString = weightTextField.text! as String as NSString
w = strW.floatValue
let strH:NSString = heightTextField.text! as String as NSString
h = strH.floatValue
bmi = w / (h * h)
var str = ""
switch (bmi)
{
case 0...18.5:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Underweight") as String
case 18.5...24.9:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Normal") as String
case 25...29.9:
str = NSString(format: "Your BMI is %.2f ", bmi, ". You are Overweight" ) as String
default:
str = NSString(format: "Your BMI is %2.f ", bmi, ". You are Obese") as String
}
resultLabel.text = str
}
}
}