Repaz Kung Repaz Kung - 1 month ago 6
Swift Question

Save number in Label even when restart app

I'm trying to save the number I tapped on the button which is shown on the label. I want the number you tapped on the button to save in the label even when I close the app and add the additional tapps to the saved number.

import UIKit
import iAd

class ViewController: UIViewController, ADBannerViewDelegate {

@IBOutlet var TapLabel: UILabel!
@IBOutlet var Banner: ADBannerView!

var taps = 0

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

Banner.hidden = true
Banner.delegate = self
self.canDisplayBannerAds = true


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true

func bannerViewDidLoadAd(banner: ADBannerView!) {
Banner.hidden = false

@IBAction func Button(sender: UIButton){

taps += 1
TapLabel.text = "Taps: \(taps)"




Use NSUserDefault for this purpose like this:

To save an integer to a NSUserDefault you can use the following code:

NSUserDefaults.standardUserDefaults().setInteger(textField.text, forKey: "key")

To get the value from the saved NSUserDefault use the following code:


To set the taps, do the following

@IBAction func Button(sender: UIButton){
    taps += 1
    TapLabel.text = "Taps: \(taps)"
    NSUserDefaults.standardUserDefaults().setInteger(taps, forKey: "key")

To get the taps, just do this in your viewDidLoad function

TapLabel.text = String(NSUserDefaults.standardUserDefaults().integerForKey("key"))