Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the first time a UIViewController appears

Before resorting to custom flags I wanted to check this with you people.

Is there a built-in way to determine in viewWillAppear: or viewWillDisappear: whether the UIViewController is 'newly pushed' onto a UINavigationController's stack, or whether the navigation controller has popped another view off, revealing this one? I want to execute some one-off code the first time the view appears. Because the self.bounds property needs to be set, the viewDidLoad: method is not the right place.

I was hoping to get some results with what I'm using to determine something similar on viewWillDisappear:, but on the first viewWillAppear: the index is already set to 1:

[self.navigationController.viewControllers indexOfObject:self]

So this is a useless snippet. Any ideas?

like image 711
epologee Avatar asked Nov 05 '22 20:11

epologee


1 Answers

The way I handle this is as follows... Have a flag which is initially set to false, when you push a child view controller onto your navigation controller set this flag to true, no on the viewDidAppear if the flag is false then it is the initial load if true then it is being returned to from a child controller popping off the navigation stack.

like image 178
Simon Lee Avatar answered Nov 12 '22 10:11

Simon Lee