Detect Taps on UITabBarController and Determining Class Type

I’ve been working on an application which uses a UITabBarController as the primary means of navigation. One of the tabs leads one to a UIViewController that displays a UIWebview as the main user interface.

The browser based navigation in the webview is based on a legacy (read WAP) website that has a great deal of content, however, it has yet to be updated to take advantage of current browser technology.

The client asked if when a user is on the tab with the browser showing, and would like to return to the home page, rather than navigating with browser-based back buttons, could one tap on the tabbar (again) to reload the main webpage?

Detecting Taps on UITabbarController

To make this happen, I needed some code to determine when a tabbar item is tapped – first, make sure your class implements the UITabBarControllerDelegate protocol:

// Implement the UITabBarControllerDelegate protocol
@interface MainTabBarController : UITabBarController  <UITabBarControllerDelegate>
{
...
}

Set the delegate in the implementation file:

// In the initialization section, set the delegate
- (id) init
{
  self = [super init];
  if (self) 
  {
    self.delegate = self;
    ....
  }
  return self;
}

The next step is to implement the following method in the class that is the delegate, which will be called when an item in the tabbar has been selected:

// User tapped on an item...
- (void)tabBarController:(UITabBarController *)tabBarController 
    didSelectViewController:(UIViewController *)viewController
{
  ...
}
Which Tab was Tapped?

So we now know when the tabbar is activated, the last step is to figure out which tab was selected. The answer lies in the second parameter to the method above, the UIViewController. As you know, when creating a tabbar, each tab is associated a view controller, so determine which tab was tapped, we can compare the view controller to see if it is an instance of given class, we do this using isKindOfClass.

Determine Class Type: isKindOfClass

isKindOfClass will indicate if the receiver is an instance of particular class (or a class that inherits from that class). For example, let’s assume we are interested in knowing when the fourth tab was selected, and as a further assumption, the fourth tab is a view controller with the type TabFourViewController.

Using isKindOfClass, here is how we can check if the view controller tapped is of TabFourViewController type:

// User tapped on an item...
- (void)tabBarController:(UITabBarController *)tabBarController 
    didSelectViewController:(UIViewController *)viewController
{
  // Is this the view controller type we are interested in?
  if ([viewController isKindOfClass:[TabFourViewController class]])
  {
    // Cast the view controller to the appropriate type
    [(TabFourViewController *) viewController updateView];
  }
}

For my project, if the types matched I called a method updateView which would reload the webview. Notice the type cast, without this you will get a compile time warning, as the compiler has no means to know if the method (updateView) is associated with the view controller passed into the method unless you specify the class type.

  1. I was able to use the Interface Builder to set the delegate for my TabController. I dragged from the delegate outlet box to the ApplicationDelegate item in the nib. It all works :-)

Comments are closed.