R – viewDidAppear: not firing under certain conditions

cocoa-touchiphoneuiviewcontroller

I have the following items in my app nib:

  • the usual: file's owner, first responder window, delegate
  • View Controller "a"
    • View "b"
      • UIScrollView "c"
      • some other stuff in "b"

In my AppDelegate applicationDidFinishLaunching, I do this:

  1. [window makeKeyAndVisible]
  2. [window addSubView:a.view];
  3. create a view controller "d"
  4. create a navigationController "e" with rootviewcontroller "d"
  5. invoke [c addSubView:e.view]

Question/problem: when I do all of the above, viewDidAppear: is not firing for "d". (but viewDidLoad IS firing.) How do I find out why it is not firing, and fix it so that it would fire?

(Why I want to use viewDidAppear: the above involves some chained animations and viewDidAppear looks like a good place for a view controller to know when its view has been loaded and animated, so it can trigger subsequent animations.)

Best Answer

Usually when you're manually screwing with the view hierarchy you won't get -viewWillAppear:, -viewDidAppear, etc.; they're called by various SDK methods, like -pushViewController:animated:, -presentModalViewController:animated:, and by UITabBarController when a tab gets selected.

When you add a view to the hierarchy yourself, it may or may not be onscreen or going-to-be-onscreen; the -addSubview: method doesn't make any assumptions about your intentions. Just call 'em yourself as you add the view.