Objective C: How to reload a view controller's table view when tab is selected
I need t开发者_JS百科o reload the data in a view controller when it's tabbar is clicked.
I am using the UITabBarControllerDelegate method as below:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 3)
{
[(SomeViewController *)viewController getData];
}
}
where 'getData' is an instance method in SomeViewController class. However when I run my app, I get the following error
2011-07-01 02:12:11.193 onethingaday[19169:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController getData]: unrecognized selector sent to instance 0x600d500'
Can anyone advise me how I can overcome this issue? I just need to trigger the 'getData' method when tabbarcontroller.selected index ==3
It seems to me from the error message you get, that you use a UINavigationController
in your tab controller; in this case, you cannot send directly the getData
message to it; you should first find out which view controller under the UINavigationController
should receive that message. (This is not actually related to the tab bar selectedIndex
)
I don't know how your UINavigationController is organized, but you could do:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
//-- option 1: getData goes to the first view controller in the UINavigationController:
[[(SomeViewController*)[(UINavigationController*)viewController topViewController] getData];
//-- option 2: getData goes to the last view controller in the UINavigationController (the visible one):
[[(SomeViewController*)[(UINavigationController*)viewController visibleViewController] getData];
}
}
If you give more details about the organization of your UINavigationController
I can help further identifying the right option.
Anyway, as you can see from the casts, there is something that is not fully ok with your design. I would strongly suggest using a notification for that. I.e., your SomeViewController
registers itself for a notification of a given type :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someSelector:)
name:ShouldGetDataNotification
object:nil];
and the tab bar controller sends the notification for your controller to react upon:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
[[NSNotificationCenter defaultCenter] postNotificationName:ShouldGetDataNotification object:nil];
}
....
}
Look at this post.
See the solution to InterfaceBuilder - UIViewController subclass not recognized as subclass
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 3)
{
[[[(UINavigationController *)viewController viewControllers] objectAtIndex:2] getData];//2 for 3rd tabbar since 0,1,2
}
}
You could implement the -viewWillAppear
method in your UITableViewController
subclass. That should be called automatically when the UITabBarController
switches to the view. It should look something like this:
- (void)viewWillAppear {
[super viewWillAppear];
[self getData];
}
精彩评论