Display a modal UIViewController when a UITabBarItem is pressed

Suppose the following scenario. You need to display a modal UIViewController whenever the user presses a specific button in a UITabBar.

The trick is pretty simple. First, add an empty UIViewController, wich will serve as placeholder for the ‘modal tab’:

[cc lang=”objc”]
UIViewController* someViewController    = [[UIViewController alloc] init];
[someViewController setTitle:NSLocalizedString(@”Modal Tab”, nil)];
[someViewController.tabBarItem setImage:[UIImage imageNamed:@”modal.png”]];
[/cc]

Then, setup the UITabBarController’s delegate:

[cc lang=”objc”]
[_tabBarController setDelegate:self];
[/cc]

At last… you need to implement the UITabBarControllerDelegate protocol. Specifically, something that looks like this:

[cc lang=”objc”]
– (BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController*)viewController

{
BOOL isModalTab = ([[viewController title] isEqualToString:NSLocalizedString(@”Modal Tab”, nil)]);

if(isModalTab)
   {
UIViewController* modalViewController = [[[ModalViewController alloc] init] autorelease];
[self presentModalViewController:modalViewController animated:YES];
}

return !isModalTab;
}
[/cc]

That would do the trick..!