Tamby Kojak
Tamby Kojak

Reputation: 2159

Do I have to reset variables in my ViewController?

I have ViewControllerA and ViewControllerB.

In Storyboard, ViewControllerA transitions to ViewControllerB once a button is clicked.

In ViewControllerB there is another button that increments the value count.

No matter the value of count, when the user presses the back button and comes back to ViewControllerB, I would assume that count have the value of 0 again, but it remains at the previously incremented value.

Why is this happening? Should I be resetting the value to 0 each time in viewWillAppear or something?

In case it matters: the variable is not a property, it is declared at the top of ViewControllerB.m as int count = 0;.

Upvotes: 0

Views: 254

Answers (1)

Scrungepipes
Scrungepipes

Reputation: 37581

"In case it matters: the variable is not a property, it is declared at the top of ViewControllerB.m as int count = 0;"

Yes it does matter - your variable has nothing whatsoever to do with ViewControllerB so therefore it won't get reset. When the view controller is loaded it is not the .m file that gets loaded each time! It is the view controller within it.

Your variable is a global variable and its lifetime is that of the app not of the view controller.

Upvotes: 1

Related Questions