Search code examples
iosobjective-cuitabbarcontrolleruiimagepickercontroller

Tab Index Correct in App Delegate Class but Not in App Delegate object


I have a tab based application. One of the tabs is a camera. When the user selects cancel, I would like for the app to return to the tab it was previously on. I am doing this by saving the previous tab index in my app delegate class. In shouldSelectViewController of my app delegate class, it logs the correct index. However, in my camera class, it always says that the previous index was 0.

    @interface LeafletAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate, UITabBarDelegate> {

        NSManagedObjectModel *managedObjectModel;
        NSManagedObjectContext *managedObjectContext;       
        NSPersistentStoreCoordinator *persistentStoreCoordinator;
        UIWindow *window;

    }

    @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
    @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
    @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    @property (strong, nonatomic) UITabBarController *tabBarController;
    @property (nonatomic, assign) NSUInteger previousTabIndex;

//Appdelegate.m 
#import "CameraViewController.m"
#pragma mark UITabBarController Delegate Methods
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.previousTabIndex = tabBarController.selectedIndex;
    NSLog(@"this is previous tab index: %lu", (unsigned long)self.previousTabIndex);
    return YES;
}


//CameraViewController.m 
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    LeafletAppDelegate *appDelegate = (LeafletAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"previous tab index: %lu", (unsigned long)appDelegate.previousTabIndex);
    [self.tabBarController setSelectedIndex:appDelegate.previousTabIndex];
}

Why would it print the correct index in my app delegate class, but not when I access that property in the app delegate object in my camera class?


Solution

  • Don't use AppDelegate for that. As the class name says, it's only for application delegation.

    You gotta create a sub class from UITabBarController, like :

     @interface LeafletTabBarController : UITabBarController <UITabBarControllerDelegate>
    

    Notice that this class is implementing UITabBarControllerDelegate, so you need to set self.delegate = self inside viewDidLoad.

    Other thing, in this sub class you can create your property @property (nonatomic) NSUInteger previousTabIndex and for the cancel action, inside your view controller:

    LeafletTabBarController *tabBarController = (LeafletTabBarController*)self.tabBarController;
            [tabBarController tabBar:tabBarController.tabBar didSelectItem:tabBarController.tabBar.items[tabBarController.previousTabIndex]];
    

    Don't forget to set your current tab bar class, and don't forget to update the previousTabIndex inside - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController at your LeafletTabBarController;

    Hope it helps!