2

I have a UITabBarController as part of my app delegate and I want to trap when the user touches a specific tab (the favourites) and force the table within it to reload the data.

What would be best practice in this instance?

I have added the UITabBarDelegate protocol to my app delegate and implemented the didSelectViewController method. So far so good. Within the method I get a viewController, so I can check its title, etc. to determine which tab is selected.

How can I then send a reloadData message to the UITableView in the viewController? I tried creating a method in my FavouritesViewController class and calling that but it does not work.

Example code:

#pragma mark UITabBarController delegate methods

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   // If the favourites tab is pressed then reload the data
   if (viewController.title = @"Favourites")
   {
      if ([viewController respondsToSelector:@selector(reloadFavourites:)]) 
      {
         [viewController reloadFavourites];
      }
   }
} 
Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81

2 Answers2

3

It sounds like you need to add a [UITableView reloadData] to the tableViewController's viewWillDisplay method. This will cause the table to reload every time it is displayed.

If you want to force a reload will the tableview is already displayed, then calling reload from the method you created in the OP should work.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • 1
    That is great thanks TechZen... Did as you said and put this in my viewController: - (void)viewWillAppear:(BOOL)animated { [favouritesTableView reloadData]; [super viewWillAppear:animated]; } and all works well, so no need to do what I was trying to do above. Thanks again, Dave – Magic Bullet Dave Dec 05 '09 at 16:19
1

The following piece of code should refresh your table every single time the view appears.

-(void)viewWillAppear:(BOOL)animated
{
    [[self tableView] reloadData];
}
aryland
  • 133
  • 1
  • 8