Search code examples
swiftbuttonvar

How do I save the value of a variable?


Here is a brief explanation of my code:

var myVar = 0

if (button image is equal to "mybutton image") {
    myVar = 1
    print("It works")
} else {
    myVar = 2
    print("Not working")
}

ButtonPress {
    print(myVar)
}

The problem here is that when I run the application I see in the console that it prints "it works" but when I press the button myVar is equal to 0 again. How do I make sure when I press the button myVar will be equal to 1?

Here is the full code as requested:

@IBAction func ThingyButtton(_ sender: UIButton) {
    if let ButtonImage = myButton.image(for: .normal),
       let Image = UIImage(named: "LoseWeightGreen.png"),
       UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
        thingy = 1
        print("1")
    } else {
        thingy = 2
        print("2")
    }
}

@IBAction func ButtonThingyN(_ sender: UIButton) {
    if(thingy == 1) {
        print("ok")
        let storyboard = UIStoryboard(name: "let", bundle: nil)
        let secondVC = storyboard.instantiateViewController(withIdentifier: "FirstVC")as! FirstVC
        self.navigationController?.pushViewController(secondVC, animated: true) 
    } else{
        print("good")
        let storyboard = UIStoryboard(name: "let", bundle: nil)
        let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondVC")as! SecondVC
        self.navigationController?.pushViewController(secondVC, animated: true)  
    }    
}

Solution

  • As i explained in my comment you need a little different logic here try using switch instead of if else as followed,

    @IBAction func ThingyButtton(_ sender: UIButton) {
        let defaults = UserDefaults.standard
        if let ButtonImage = myButton.image(for: .normal),
           let Image = UIImage(named: "LoseWeightGreen.png"),
           UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
            defaults.set(1, forKey: "thingy")
            defaults.synchronize()
            print("1")
        } else {
            defaults.set(2, forKey: "thingy")
            defaults.synchronize()
    
            print("2")
        }
    }
    
    
    
    @IBAction func ButtonThingyN(_ sender: UIButton) {
        let defaults = UserDefaults.standard
        if defaults.object(forKey: "thingy") != nil {
              let thingy = defaults.integer(forKey: "thingy")
              let storyboard = UIStoryboard(name: "let", bundle: nil)
              switch thingy{
                  case 1:
                      var moveTo = storyboard.instantiateViewController(withIdentifier: "FirstVC") as! FirstVC
              self.navigationController?.pushViewController(moveTo, animated: true)
    
                      break
                  case 2:
                     var moveTo = storyboard.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
              self.navigationController?.pushViewController(moveTo, animated: true)
    
                      break
                  default:
                      print("Thingy value is \(thingy)")
                      return
              }
         }
    
    
    
    }