Right now I have two view controllers, and when I gather data from my second view controller I display it in a label in my first view controller (a random integer):
var FirstInteger = //random integer
override func viewDidLoad() {
super.viewDidLoad()
TotalAmount.text = String(FirstInteger)
This works fine, but when the user goes back to the second view controller and gets a new random integer, I want to add this integer to the first one gathered, however many times the user goes back to the second view controller and "gets" a new integer- so I decided to do something like this:
var FirstInteger = //random integer
var NewInteger = 0
override func viewDidLoad() {
super.viewDidLoad()
TotalAmount.text = String(FirstInteger + NewInteger)
NewInteger = FirstInteger + NewInteger
But I know this doesn't work, since everytime the user would go back to the first view controller, it would reset the NewInteger's value to 0- does anyone know how I could fix this?
You need to declare the NewInteger as static something like this
This will create only one variable newInteger which will be at class level (or Type level as SWIFT calls it) you should be able to access it like this in your code
Read through this for more details on the variable types and their life cycles in SWIFT
Type Properties