0

I made an app Single Application. I put a button called CountUpButton, a label called CountLabel, and another button ResetButton. The function of CountUpButton is when button is pressed, the CountLabel has to change + 1 and when ResetButton is pressed, CountLabel has to turn to 0. But the problem is that whenever I reboot my iPhone or App, CountLabel turns back to 0. It's supposed CountLabel to be saved, so it have to change only when ResetButton is pressed.

The code of This function is:

var CountNumber = 0

@IBAction func ResetButton(sender: UIButton) {
    CountNumber = 0
    CountLabel.text = "0"
}

@IBOutlet var CountLabel: UILabel!

@IBAction func CountUpButton(sender: UIButton) {
    CountNumber += 1
    CountLabel.text = "\(CountNumber)"
}

and code that I used for "CountLabel" to be saved is:

func saveCounted(){
    let defeaults = NSUserDefaults.standardUserDefaults()
    defeaults.setInteger(CountNumber, forKey: "CountLabel")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Emm
  • 1,963
  • 2
  • 20
  • 51
  • Where do you *read* the value from the saved user defaults? – Martin R Jul 09 '15 at 17:42
  • Ya I noticed it, I tried to fix this but I don't know how, I'm talking for forKey: "CountLabel" – Emm Jul 09 '15 at 18:15
  • In your viewDidLoad, put `CountNumber = NSUserDefaults.standardDefaults().IntegerForKey("CountLabel")` to initialize it with the value stored in the defaults. – Adam Evans Jul 09 '15 at 18:20
  • In addition to what Martin asked, do you actually call the `saveCounted` function? – rmaddy Jul 09 '15 at 18:21
  • Ya I Did. override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. saveCounted() } – Emm Jul 09 '15 at 18:25

1 Answers1

1
import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var counterLabel: UILabel!
    var counter: Int {
        return NSUserDefaults().integerForKey("counter")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        counterLabel.text = "\(counter)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func countUp(sender: AnyObject) {
        NSUserDefaults().setInteger(counter+1, forKey: "counter")
        counterLabel.text = "\(counter)"

    }
    @IBAction func resetCounter(sender: AnyObject) {
         NSUserDefaults().removeObjectForKey("counter")
        counterLabel.text = "\(counter)"
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • It's not working bro, with this code is adding all counted numbesr so when app is loaded it shows all counted numbers added, ex: first time I counted 10, second time 20 and third time 20 and if I load app it shows 50. It's supposed to show last counted number. – Emm Jul 10 '15 at 02:05
  • try to add a reset button, You will see what happens – Emm Jul 10 '15 at 02:12
  • I will add a reset button for you – Leo Dabus Jul 10 '15 at 02:12
  • 1
    Bro Thanks I will rate this Answer now, I made a little change in code so now it works, Thanks (Y) – Emm Jul 10 '15 at 02:20