ios - Keeping a view controller in it's state after you leave and come back to it- Swift -
right have 2 view controllers, , when gather data second view controller display in label in first view controller (a random integer):
var firstinteger = //random integer override func viewdidload() { super.viewdidload() totalamount.text = string(firstinteger)
this works fine, when user goes second view controller , gets new random integer, want add integer first 1 gathered, many times user goes second view controller , "gets" new integer- decided this:
var firstinteger = //random integer var newinteger = 0 override func viewdidload() { super.viewdidload() totalamount.text = string(firstinteger + newinteger) newinteger = firstinteger + newinteger
but know doesn't work, since everytime user go first view controller, reset newinteger's value 0- know how fix this?
you need declare newinteger static
class myclass { static var newinteger = 0 }
this create 1 variable newinteger @ class level (or type level swift calls it) should able access in code
myclass.newinteger = firstinteger + myclass.newinteger
read through more details on variable types , life cycles in swift
Comments
Post a Comment