Cooper Edmunds
Cooper Edmunds

Reputation: 47

Calling the viewWillAppear after the first time

I have a game that uses a timer. The user has 10 seconds to do something. I have a pause button in it and I am trying to set how much time is left in the viewWillDisappear and then set the label with how much time is left in the viewWillAppear. So that if there was 6 seconds left when the user paused, when they resumed, the viewWillAppear would be executed and the label would be at 6 seconds. But the problem is, the viewWillAppear is being executed when the user first starts that level, which completely messes up the time. Is there any way I could only execute the viewWillAppear after the first time? Thank you.

Upvotes: 0

Views: 659

Answers (1)

Hermann Klecker
Hermann Klecker

Reputation: 14068

Your observation is correct. viewWillAppear is called every time when the view will appear. Naturally it is called before the first appearance too.

What yould you do? viewDidLoad is called only once. It is called before viewWillAppear will be called. You could initialize the iVar with the remaining time to 10s in viewDidLoad. Use the same iVar (or what ever you are using) as you would in viewWillDisappear. In the first appearance viewWillAppear will be called straight away so that re-setting the counter to the saved value of remaining time would not do any harm to your logic.

Upvotes: 0

Related Questions