Pergunta

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

Foi útil?

Solução

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.

Outras dicas

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.

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;
}

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?

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top