Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidAppear called twice in iOS5

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

like image 855
Ibai Avatar asked Dec 27 '11 15:12

Ibai


People also ask

Can viewDidAppear be called multiple times?

viewDidAppear callback can be called multiple times in a single presentation.

How many times viewDidAppear is called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.

What is called after viewDidAppear?

viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded.


1 Answers

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.

like image 182
thgc Avatar answered Sep 28 '22 00:09

thgc