Storing number value

Hello friends :smile: , I am working on a golf app and a feature is a stroke counter for each hole.
This is the code for adding and removing strokes, however every time I change the view controller, the number resets.
Could anyone point me in the right direction regarding somehow storing the number? Thanks!

  • Matej
    —Code—
import UIKit

class Hole2: UIViewController {

    @IBOutlet weak var StrokeCounter2: UILabel!
    
    @IBAction func plus2() {
        guard let presentValue2 = Int(StrokeCounter2!.text!), presentValue2 < 9 else { return }

        let newValue2 = presentValue2 + 1
        StrokeCounter2!.text = String(newValue2)
        
    }
    @IBAction func minus2() {
        guard let presentValue2 = Int(StrokeCounter2!.text!), presentValue2 > 0 else { return }

        let newValue2 = presentValue2 - 1
        StrokeCounter2!.text = String(newValue2)
        
       }
    
override func viewDidLoad() {
    super.viewDidLoad()
}
}

You could save the value in user defaults or use protocol / delegates to pass the value back and fourth between VCs

1 Like

Well I don’t want it in user defaults as the user will change the values every round… I need them saved for one session