views:

674

answers:

1

Hi folks,

I want simply to refresh my tableView (on the iPhone) when I click on the related button in the tabBar...

So, I think this has to be done this way :

[self.tableView reloadData];

Right ? And done in the

(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

But I don't know how can I send the message reloadData to the tableView from the tabBar controller... Could you help me please ?

+3  A: 

In your UITableViewController class, implement the viewWillAppear method and call reloadData from there.

Ian Henry
good idea :) thank you ! (I'm gonna try it right now)
Archanimus
Works fine (thx again ;) ) But if I want to refresh the tableView when clicking to the related tabBar button EVEN if i'm already in the tableView ? (in other words, using the tabBar button as a refresh button)
Archanimus
In v3.0 and up of the SDK, you can do this by implementing the `UITabBarControllerDelegate` class (somewhere) and responding to the `- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController` method. In v3.0 and below, this is not possible, as that method is not called unless the selected view controller actually changes.
Ian Henry
Oh, and setting the `UITabBarController`'s `delegate` property accordingly.
Ian Henry
Yes, this was my first idea (in the question) but I my (very likely simple) question is how to access the methods of my actual viewController. (because the parametre viewController doesn't respond to the method calls)And thank you :)
Archanimus
Manually call the `viewWillAppear` method from within `tabBarController:didSelectViewController:`, which in turn will call the `reloadData` method of its table. Or set up a dummy method in your `viewController` that just calls `reloadData` and does nothing else. Or cast the `viewController` as a `UITableViewController` in the delegate's method (it will still respond to the selector without this, eg `[[viewController tableView] reloadData]`).
Ian Henry
My problem is that I don't manage to call viewController methods from the tabBarController:didSelectViewController: , it crashes with unknown selector :| (even with [[viewController tableView] reloadData] )
Archanimus
And your viewController has a `tableView` property that's being synthesized? The only other thing I can think of is that you're sending that selector to _other_ viewControllers that aren't `UITableViews` -- are checking to make sure you're only sending it to that one?
Ian Henry
Well, when I debug and I put a breakpoint in tabBarController:didSelectViewController, I see that viewController has an (NSCFArray *) _viewControllers that contain my actual tableView. And when I make the test if ([viewController isKindOfClass:[MyTableViewController class]])It never gives "true" ... :sThank you very much for your help
Archanimus