带有分段控制的未解析标识符的使用

我在我的应用程序中有一个场景,我想在两个滑动条和用户选择的分段控件之间进行基本的数学运算。 我试图在分段控件下的变量之间进行数学运算,但Xcode给了我一个(!)说明

使用未解析的标识符“xxxx”

与我试图添加的任何变量。 我的代码是:

import UIKit

class DopamineCalculator: UIViewController {

//slider outlets
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var sliderone: UISlider!
//segmentoutlet
@IBOutlet weak var segmentoutlet: UISegmentedControl!


//LABELS
//weight label
@IBOutlet weak var weightlabel: UILabel!
//dosage label
@IBOutlet weak var dosagemg: UILabel!
 //Drip Rate Answer Label
@IBOutlet weak var dripanswer: UILabel!




//ACTIONS!!
//weight slider action
@IBAction func weightslider(sender: UISlider) {

    let weight = Int(sender.value)
    //converts to kgs
    let kgs = Int(sender.value) / Int(2.2)

    weightlabel.text = "(weight) lbs"
}


//Dosage Desired dosage slider

@IBAction func dosagedesired(sender: UISlider) {

    let dosage = Int(sender.value)



    dosagemg.text = "(dosage) mg"
}


//CONCENTRATION OPTIONS
@IBAction func concentrationoption(sender: UISegmentedControl) {
        switch segmentoutlet.selectedSegmentIndex
        {
        case 0:


            dripanswer.text = (kgs) * (dosage) / 800


        case 1:


            dripanswer.text = "1600";



        case 2:


            dripanswer.text = "3200";

        default:
            break;
    }
}

我想要做的是每种情况分别为kgs x剂量/ 800。 我找不到解决标识符问题的在线解决方案。


@IBOutlet ...

// Add variables to be reachable for all methods    
var dosage : Int = 0
var kgs : Double = 0.0
var divisor : Double = 800.0 

@IBAction func dosagedesired(sender: UISlider) {

    // prepend self to variable as it is used
    self.dosage = Int(sender.value)
    dosagemg.text = "(self.dosage) mg"
    // adding the calculation here
    let result = self.kgs * Double(self.dosage) / self.divisor
    dripanswer.text = "(result)"
}

@IBAction func weightslider(sender: UISlider) {
    let weight = Int(sender.value)    
    // converts to kgs
    // the same here
    self.kgs = Int(sender.value) / Int(2.2)
    // adding the calculation here
    let result = self.kgs * Double(self.dosage) / self.divisor
    dripanswer.text = "(result)"
}

@IBAction func concentrationoption(sender: UISegmentedControl) {

    switch segmentoutlet.selectedSegmentIndex
    {
        case 0:                
            self.divisor = 800.0            
        case 1:
            self.divisor = 1600.0
        case 2:
            self.divisor = 3200.0
        default: 
            self.divisor = 800.0
    }
    // finally the calculation 
    let result = self.kgs * Double(self.dosage) / self.divisor
    dripanswer.text = "(result)"            
}

您应该在任何方法之外,即在课堂级别以及您的网点申报变量kgsdosage

//slider outlets
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var sliderone: UISlider!
//segmentoutlet
@IBOutlet weak var segmentoutlet: UISegmentedControl!

//LABELS
//weight label
@IBOutlet weak var weightlabel: UILabel!
//dosage label
@IBOutlet weak var dosagemg: UILabel!
 //Drip Rate Answer Label
@IBOutlet weak var dripanswer: UILabel!

// You should declare the variables here:
var kgs = 0
var dosage = 0

当你在方法中使用kgsdosage ,删除let这个词是因为你没有声明变量。

你这样做的原因是你不能在另一个方法中访问在方法中声明的变量。 在concentrationoption方法中,您无法获得dosage ,这在dosagedesired方法中定义。

链接地址: http://www.djcxy.com/p/36293.html

上一篇: Use of Unresolved Identifier with Segmented Control

下一篇: How to convert a string with the name of a class to the class type itself?