问题
the following statement is returning an nil...
NSArray *viewControllersInNavStack = [[self navigationController] viewControllers];
and this statement is being called in a view controller that's about four levels deep in the navigation stack, so I don't understand how or why it would ever return nil. Your thoughts?
Thanks so much for your wisdom!
回答1:
Doesn't sound like its your issue, but docs say "Only returns a navigation controller if the view controller is in its stack. This property is nil if a navigation controller cannot be found".
First check if [self navigationController] is returning nil. If not, then viewControllers] must, but break it down first.
回答2:
what what point are you checking? It may well be that the self.navigationController has not been populated by the environment yet. Can you confirm that self.navigationController isnt nil 0x0?
and if thats the case, you can move your code from init (I'm assuming thats where you have it) to viewDidLoad;
回答3:
I found the problem. This statement was being called in response to an ApplicationWillResignActive notification. Well, one of the other view controllers higher up the stack was also listening for the same notification and its handler popped that view controller off the stack, along with all the other view controller deeper down the stack, including the one executed the code above. Therefor, by the time the code was executed, its view controller was no longer on the stack.
来源:https://stackoverflow.com/questions/5932137/ios-development-what-are-some-reasons-that-self-navigationcontroller-viewcon