I don’t know if there’s a better way to do this, but if someone cancelled some action in my app (say cancel sending email) I wanted to know where they came from so I could pop that view back up on the screen.

Do to that I created a variable for each view in my app:

Utils.h

#define CAMERA_VIEW 0
#define FRIEND_VIEW 1
#define LASTVIEW_FILE @"%@/viewFile"

And then whenever a view disappears I stored that last view information locally on disk (stored in an array):

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // store last view
    NSArray* lastView = [NSArray arrayWithObject:[NSNumber numberWithInt:CAMERA_VIEW]];
    [lastView writeToDocumentDirectory:LASTVIEW_FILE];
}

Then when someone cancels some action (like the sending of an email) I simply read the last view from disk, and transition there:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];
    [self transitionToLastView];
}   

- (void)transitionToLastView {
    
    NSArray* lastViewArray = [NSArray arrayWithContentsFromDefaultFile:LASTVIEW_FILE];
    NSNumber* lastViewNumber = [lastViewArray objectAtIndex:0];
    int lastViewIndex = [lastViewNumber intValue];
    
    UITabBarController *tabBarController = self.tabBarController;
    UIView * fromView = tabBarController.selectedViewController.view;
    UIView * toView = [[tabBarController.viewControllers objectAtIndex:lastViewIndex] view];
    
    [UIView transitionFromView:fromView
                        toView:toView 
                      duration:0.5 
                       options:UIViewAnimationOptionTransitionNone
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = lastViewIndex;
                        }
                    }];
}

I don’t know if there’s a better way to do this, but it works!

Note: writeToDocumentDirectory is a helper routine that stores the array at the default directory location on the iPhone.

-(void) writeToDocumentDirectory:(NSString *)filename {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullFileName = [NSString stringWithFormat:filename, documentsDirectory];
    [self writeToFile:fullFileName atomically:YES];
}