Search code examples
iphoneiostabbarcontrollerselectedindex

Make ajax request upon programmatically switching tabBarController viewContoller


I've been reading through forum posts and banging my head for over 2hrs now and hopefully someone can help point me in the right direction.

I am using the UITabBarController and have 4 root views associated to it. In the first view, the user can take a picture (UIImagePicker), after which the second view is displayed programmatically. Once the second view is displayed I want to make an ajax request to a server.

I am able to sucessfully select/display the second view with the following code:

self.tabBarController.selectedIndex = 1;

However the UITabBarControllerDelegate method (which is successfully called upon user tabBarContoller touches):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

is not called, as Apple tabBarController:didSelectViewController documentation states:

In versions of iOS prior to version 3.0, this method is called only when the selected view controller actually changes. In other words, it is not called when the same view controller is selected. In addition, the method was called for both programmatic and user-initiated changes to the selected view controller.

So my question is: "How can I execute some code & make a request after programmatically switching to second view?"

I also tried putting traces in:

-(void)viewDidAppear 
-(void)ViewWillAppear 
-(void)viewDidLoad

but none of those are called when tabBarController.selectedIndex is set.

Many Thanks


Solution

  • viewWillAppear should be called.. I just tested it.

    Another solution: use the UITabBarControllerDelegate with

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    

    for tracking changes.