ios - Variable going back to 0 when label saved -


i made app save label can make go or down 1 number in app. if lose out of app , open up, if make label go again resets 0 , goes one. because have set variable value zero. here code:

    @iboutlet var goal: uilabel!      @ibaction func player1button(sender: anyobject)     {     nsuserdefaults.standarduserdefaults().setvalue(goal.text!, forkey:"firstgoal")     }      var goal1 = 0      @ibaction func goalup(sender: anyobject)     {     goal1++     goal.text = "\(goal1)"     }      override func viewdidload()     {     super.viewdidload()     goal.text = (nsuserdefaults.standarduserdefaults().objectforkey("firstgoal") as? string)     } 

im saving goal number calling later in text. please show me way fix adds on previous number.

in viewdidload() add

goal1 = int((nsuserdefaults.standarduserdefaults().objectforkey("firstgoal") as? string)!) 

this set int variable using keep track of goals correct value when app loads. setting text label correct value.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -