views:

423

answers:

1

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];
      }
   }
}
+1  A: 

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
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
Glad to be able to help.
TechZen