Jaanus
Jaanus

Reputation: 17866

viewDidAppear: not firing under certain conditions?

I have the following items in my app nib:

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.)

Upvotes: 5

Views: 3956

Answers (3)

Raunak
Raunak

Reputation: 3414

I had a similar issue when I set the delegate of my navigation controller. So in my UINavigationControllerDelegate methods, I did something like this:

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //do something here
    [viewController viewWillAppear:animated];
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [viewController viewDidAppear:animated];
}

Upvotes: 1

Ron Srebro
Ron Srebro

Reputation: 6862

The first thing you should be aware of is that viewDidAppear is a method of UIViewController and not of UIView, it really has nothing to do with views.

The second thing is that there can only be one "active" UIViewController at a time.

When you add "a"'s view to the window it becomes the active UIViewController and only "a" will receive the viewDidAppear message while "e" won't actually be getting any UIViewContoller related methods (viewDidAppear, viewWillAppear etc.)

As @Noah mentioned when you use pushViewController you will receive these messages because the method causes the pushed view Controller to become the "active" UIViewController.

My suggestion for you is that if you create controllers for views that are subviews don't subclass UIViewController but rather NSObject, it will reduce your confusion level as you won't expect to get your UIViewController methods called which they won't anyway.

Upvotes: 3

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

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.

Upvotes: 10

Related Questions