Determine the first time a UIViewController appears
Asked Answered
M

2

6

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?

Marr answered 15/6, 2011 at 12:5 Comment(0)
K
3

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.

Kannada answered 15/6, 2011 at 12:49 Comment(1)
Yes, that's the scenario I was hoping to prevent. I'll wait a little while longer to see if anyone else comes with a more concise solution. Otherwise this is it. Thx.Marr
U
-1

There is no built in way to check it. This is the detail discussionabout your query.

Untie answered 15/6, 2011 at 12:17 Comment(1)
Actually, the question you're referring to deals with an entirely different topic.Marr

© 2022 - 2024 — McMap. All rights reserved.