Hello!
I have a variable.
var variant: Int=0 There are functions that should change the value after pressing certain buttons.
@IBAction func pohudenie(_ sender: Any) { variant = 1 print(variant) } @IBAction func podderzhanie(_ sender: Any) { variant = 2 print(variant) } @IBAction func nabor(_ sender: Any) { variant = 3 print(variant) } After that comes the next button, in which the variable should be defined via the if statement and perform one of the actions.
@IBAction func result(_ sender: Any) { if variant == 1 { kall2=(kall-(kall/100)*25) } if variant == 2 { kall2=(kall-(kall/100)*20) } else{ kall2=kall } } I dropped the extra code. The problem is that in the result function, the variant variable is either not determined, or the variable does not change to the desired one in the first three functions. As a result, the third action from else is performed. How to solve this problem?
As I understand it, this change of variable remains within those buttons.
I add all the code
import UIKit class ViewController: UIViewController { @IBOutlet weak var text1: UITextField! @IBOutlet weak var text2: UITextField! @IBOutlet weak var sextype: UISegmentedControl! @IBOutlet weak var traning: UISegmentedControl! @IBOutlet weak var activ: UISegmentedControl! @IBOutlet weak var resultlabel: UILabel! func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } var variant: Int = 0; @IBAction func pohudenie(_ sender: Any) { variant = 1 print(variant) } @IBAction func podderzhanie(_ sender: Any) { variant = 2 print(variant) } @IBAction func nabor(_ sender: Any) { variant = 3 print(variant) } let height = 170 @IBAction func result(_ sender: Any) { var kall: Double=0 var kall2: Double=0 let activity = [1.2, 1.38, 1.56, 1.73, 1.95] let selectedActiv = activity[activ.selectedSegmentIndex] let factor = [0.9, 0.95, 1, 1.03, 1.06, 1.09, 1.11, 1.13] let selectedFactor = factor[traning.selectedSegmentIndex] if let age = Int(text1.text!) { if let weight = Int(text2.text!) { switch sextype.selectedSegmentIndex { case 0: kall=(Double(selectedFactor) * (Double(selectedActiv) * (66 + (13.7 * Double(weight)) + (5 * Double(height)) - (6.8 * Double(age))))) case 1: kall=(Double(selectedFactor) * (Double(selectedActiv) * (665 + (9.6 * Double(weight)) + (1.8 * Double(height)) - (4.7 * Double(age))))) default: kall = 0 } } } switch variant { case 1: kall2=(kall-(kall/100)*25) case 2: kall2=(kall-(kall/100)*20) case 3: kall2=kall default: kall2=kall } let belki = 0.31 let zhir = 0.12 let uglevod = 0.57 let belki1 = 3.8 let zhir1 = 9.3 let uglevod1 = 4.1 var belki2: Double=0 var zhir2: Double=0 var uglevod2: Double=0 belki2=(kall2*belki)/belki1 zhir2=(kall2*zhir)/zhir1 uglevod2=(kall2*uglevod)/uglevod1 UIApplication.shared.keyWindow!.endEditing(true) resultlabel.text = "Вы должны потреблять \(Int(kall2)) килокалорий\nИз которых: \nБелков: \(Int(belki2)) g \nЖиров: \(Int(zhir2)) g \nБелков: \(Int(uglevod2)) g" } override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = .clear // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }