viewDidAppear called twice in iOS5
Asked Answered
S

5

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

Scuta answered 27/12, 2011 at 15:3 Comment(4)
You should probably just fix the underlying issue. Are you doing anything unusual when pushing the view controller?Walachia
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 ViewControllersScuta
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.Walachia
Wait, are you saying that you have another UITabBarController inside of the first tab of another UITabBarController?Carbazole
C
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.

Cadaver answered 27/12, 2011 at 15:6 Comment(3)
Isn't that a little bit dirty? I know I could solve it that way but I think there should be something more correct.Scuta
@Ibaivi - sometimes you just gotta do these things.Conceal
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.Riehl
H
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.

Hialeah answered 13/1, 2012 at 10:21 Comment(0)
L
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;
}
Lattie answered 11/5, 2012 at 12:50 Comment(0)
C
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.

Cadaver answered 27/12, 2011 at 15:6 Comment(3)
Isn't that a little bit dirty? I know I could solve it that way but I think there should be something more correct.Scuta
@Ibaivi - sometimes you just gotta do these things.Conceal
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.Riehl
I
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?

Ilarrold answered 27/12, 2011 at 20:16 Comment(0)
R
-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.

Riehl answered 27/12, 2011 at 15:7 Comment(1)
And it was calling just once in iOS5 but if I do that way, viewDidAppear will not being called in iOS4.3...Scuta

© 2022 - 2024 — McMap. All rights reserved.