Use of Unresolved Identifier with Segmented Control
I have a scene in my app that I want to do basic math between two slider bars and a segmented control that the user picks. I am trying to do the math between variables under the segmented controls, but Xcode is giving me a (!) stating
Use of unresolved identifier "xxxx"
with whatever variable I'm trying to add. My code is:
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;
}
}
What I am trying to do is kgs x dosage / 800 respectively for each case. I can not find an adequate solution online to the unresolved identifier issue.
@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)"
}
You should declare the variables kgs
and dosage
outside of any method ie at the class level, along with your outlets
//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
And when you use kgs
and dosage
inside your methods, remove the word let
because you're not declaring a variable.
The reason you do this is that you can't access variables declared in a method in another method. In the concentrationoption
method, you can't access dosage
, which is defined in dosagedesired
method.
上一篇: Android微软动态CRM adal4j登录问题
下一篇: 带有分段控制的未解析标识符的使用