ios - Save number in Label even when restart app -
i'm trying save number tapped on button shown on label. want number tapped on button save in label when close app , add additional tapps saved number.
import uikit import iad class viewcontroller: uiviewcontroller, adbannerviewdelegate { @iboutlet var taplabel: uilabel! @iboutlet var banner: adbannerview! var taps = 0 override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. banner.hidden = true banner.delegate = self self.candisplaybannerads = true } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can 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
purpose this:
to save integer nsuserdefault can use following code:
nsuserdefaults.standarduserdefaults().setinteger(textfield.text, forkey: "key")
to value saved nsuserdefault use following code:
nsuserdefaults.standarduserdefaults().integerforkey("key")
edit
set taps, following
@ibaction func button(sender: uibutton){ taps += 1 taplabel.text = "taps: \(taps)" nsuserdefaults.standarduserdefaults().setinteger(taps, forkey: "key") }
to taps, in viewdidload
function
taplabel.text = string(nsuserdefaults.standarduserdefaults().integerforkey("key"))
Comments
Post a Comment