R – Different values (nil and non-nil) for self.navigationController depending on stack location

iphoneobjective cuinavigationcontroller

I have a UIViewController subclass that accesses self.navigationController in viewDidLoad:. Depending on where it is in the view stack, I will either get the navigationController or I will get nil.

If my stack is "RootViewController -> MyViewController", I get a valid pointer.

If my stack is "RootViewController -> AnotherViewController -> MyViewController", I get nil.

In both cases, MyViewController is being pushed onto the stack with:

[self.navigationController pushViewController:self.myViewController 
                           animated:YES];

This has me stuck, because I need to be able to push another ViewController on the stack above MyViewController, which works fine in the first case but obviously fails on the second.

Best Answer

from everything I can see the UIViewController's self.navigationController is designed to always return the appropriate UINavigationController as long as the UIViewController exists within a UINavigationController's stack. This is probably one of those cases that I seem to run into myself more often than I'd like where a simple coding error is masquerading as something much more complex. In these cases I usually have good luck setting up an entirely new Xcode project with the minimal code needed to reproduce the issue and more times than not in that process I find a fix for something I overlooked that can then be easily applied in the main project. I'm sorry I can't offer more specifics but what you are describing seems to go against the design of the Cocoa Touch framework and if you can still reproduce it in a minimal Xcode project written in strict accordance with the documentation it may be a good idea to file a bug report with Apple. Hopefully though you will just find something simple you overlooked and be kicking yourself in a few minutes =)

Related Topic