iOS app error – Can’t add self as subview

I am speculating based on something similar that I debugged recently…
if you push (or pop) a view controller with Animated:YES it doesn’t complete right away, and bad things happen if you do another push or pop before the animation completes. You can easily test whether this is indeed the case by temporarily changing your Push and Pop operations to Animated:NO (so that they complete synchronously) and seeing if that eliminates the crash.
If this is indeed your problem and you wish to turn animation back ON, then the correct strategy is to implement the UINavigationControllerDelegate protocol.
This includes the following method, which is called after the animation is complete:

navigationController:didShowViewController:animated:

Basically you want to move some code as needed into this method to ensure that no other actions that could cause a change to the NavigationController stack will occur until the animation is finished and the stack is ready for more changes.

Leave a Comment