6

I'm developing an app with an UINavigatorController. I am using the method viewDidAppear in the second pushed viewController to find information in an external server.

Well. While in iOS5 worked fine at the beginning, I realized that viewDidAppear was not being called in iOS4.3 so I put this code in the root:

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

Thereafter, the app started to work properly in iOS4.3. However, in iOS5 didn't because it's calling twice viewDidAppear (the one which was being called at first and the one from the navigationController:didShowViewController:animated:)

What should I do to have only called once viewDidAppear?

Thank you very much

Ibai
  • 568
  • 1
  • 7
  • 21
  • You should probably just fix the underlying issue. Are you doing anything unusual when pushing the view controller? – Firoze Lafeer Dec 27 '11 at 16:05
  • No Firoze, I've tried everything but viewDidAppear and viewWillAppear are not being called in any viewController from the NavigationController. I've got a UITabViewController and a UINavigationController inside the first tab which loads several ViewControllers – Ibai Dec 27 '11 at 16:12
  • To be honest, every single time I've seen a project where those lifecycle methods were unreliable, it was because of common mistakes in the structure or presentation of the container controller. Have someone look at that code. I'm sure you can make it work consistently in ios4 with no funny hacks. – Firoze Lafeer Dec 27 '11 at 17:00
  • Wait, are you saying that you have another `UITabBarController` inside of the first tab of ***another*** `UITabBarController`? – Mark Adams Dec 27 '11 at 20:18

5 Answers5

4

The only real solution I see (or rather workaround for iOS 4.x) if you set some kind of state in your viewWillAppear-call and check whether it's been set or not in subsequent calls, e.g.

-(void)viewWillAppear:(BOOL)animated {
    if (!viewWillAppearCalled) {
        viewWillAppearCalled = YES;

        /* do stuff */
    }
}

Then you could safely call it manually for compatibility with iOS 4.x .

Same thing can be done for viewDidAppear, viewWillDisappear and viewDidDisappear.

gchbib
  • 1,955
  • 1
  • 17
  • 23
2

You probably have another issue (why the viewDidAppear is not being called on iOS 4).

However, I ran into the inconsistency between iOS 5 and iOS 4 in this respect as well because I used a custom container view controller (neither UINavigationController nor UITabBarController). The fix to restore iOS 4 compatibility was to implement the following method in the container view controller:

- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
    return NO;
}
Werner Altewischer
  • 10,080
  • 4
  • 53
  • 60
1

Check which version of the iOS the user is running by using [[UIDevice currentDevice] systemVersion]; and in case it's 4.3, call the viewDidAppear method.

Fernando Valente
  • 1,096
  • 1
  • 8
  • 30
  • 2
    Isn't that a little bit dirty? I know I could solve it that way but I think there should be something more correct. – Ibai Dec 27 '11 at 15:14
  • @Ibaivi - sometimes you just gotta do these things. – Peter M Dec 27 '11 at 15:23
  • Instead of checking for the version using `systemVersion`, better check for the existance of a class, or a class responding to a selector that was added on the OS you want to target. – pgb Mar 16 '12 at 12:27
0

If it is being called twice and you were only able to make the call when you added the code to the root navigation, why not remove the code from viewDidAppear (the first one you made that worked on iOS5) and leave only the one that worked in both 4.3 and 5?

Kinetic Stack
  • 788
  • 12
  • 33
-1

You should not be calling viewDidAppear: manually, leave it up to UIKit to call it for you. If you remove the manual call, it should be called only once.

pgb
  • 24,813
  • 12
  • 83
  • 113
  • And it was calling just once in iOS5 but if I do that way, viewDidAppear will not being called in iOS4.3... – Ibai Dec 27 '11 at 15:17