我创建了两个浮点变量:
var yourWeight = (yourWeightTextField.text as NSString).floatValue
var calorieNumber = (calorieNumberTextField.text as NSString).floatValue
我收到了以下错误:
// 'ViewController' does not have a member named 'yourWeightTextField'
// 'ViewController' does not have a member named 'yourWeightTextField'
我已经设置了我的IBOutlets:
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var calculatorButton: UIButton!
@IBOutlet weak var inspirationLabel: UILabel!
@IBOutlet weak var beginningLabel: UILabel!
@IBOutlet weak var calculatorContainer: UIView!
@IBOutlet weak var answer1Label: UILabel!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var yourWeightTextField: UITextField!
@IBOutlet weak var exerciseListPickerView: UIPickerView!
@IBOutlet weak var calorieNumberTextField: UITextField!
@IBOutlet weak var menuExampleButton: UIButton!
@IBOutlet weak var aboutButton: UIButton!
@IBOutlet weak var calculateButton: UIButton!
这是我的ViewDidLoad方法:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
yourWeightTextField.delegate = self
calorieNumberTextField.delegate = self
exerciseListPickerView.delegate = self
exerciseListPickerView.dataSource = self
calculateButton.enabled = false
// Calling the textfield valueChanged Methods
yourWeightTextField.addTarget(self, action:"yourWeightEditingChanged:", forControlEvents:.EditingChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberEditingChanged:", forControlEvents:.EditingChanged);
}
以下是代码的其余部分:
var yourWeight = (yourWeightTextField.text as NSString).floatValue
var calorieNumber = (calorieNumberTextField.text as NSString).floatValue
var exerciseCurrentValue:Float = 0.009
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch (row) {
case (0):
exerciseCurrentValue = 0.009
case (1):
exerciseCurrentValue = 0.019
case (2):
exerciseCurrentValue = 0.023
case (3):
exerciseCurrentValue = 0.029
case (4):
exerciseCurrentValue = 0.045
case (5):
exerciseCurrentValue = 0.033
case (6):
exerciseCurrentValue = 0.038
case (7):
exerciseCurrentValue = 0.038
case (8):
exerciseCurrentValue = 0.039
case (9):
exerciseCurrentValue = 0.053
case (10):
exerciseCurrentValue = 0.061
case (11):
exerciseCurrentValue = 0.063
case (12):
exerciseCurrentValue = 0.063
case (13):
exerciseCurrentValue = 0.064
case (14):
exerciseCurrentValue = 0.076
case (15):
exerciseCurrentValue = 0.083
default:
exerciseCurrentValue = 0.009
}
}
@IBAction func calculateButtonTapped(sender: AnyObject) {
answer1Label.text = "It will take you \((exerciseCurrentValue) / ((yourWeight) * (calorieNumber))) minutes to burn off those calories by performing that exercise."
}
我还有另一个错误
@IBAction func calculateButtonTapped(sender: AnyObject) {
answer1Label.text = "It will take you \((exerciseCurrentValue) / ((yourWeight) * (calorieNumber))) minutes to burn off those calories by performing that exercise."
,但我认为这将得到修复:
// 'ViewController' does not have a member named 'yourWeight'
请提供尽可能多的代码,因为我是编程新手。谢谢!
答案 0 :(得分:2)
您无法使用实例值指定属性。
您需要像这样定义属性。
var yourWeight:Float = 0 // Or whatever default value you want
var calorieNumber:Float = 0 // Or whatever default value you want
然后在viewDidLoad中,无论你需要获取值,还是执行:
self.yourWeight = (self.yourWeightTextField.text as NSString).floatValue
self.calorieNumber = (self.calorieNumberTextField.text as NSString).floatValue