tags:

views:

683

answers:

2

I'd like to send a message to a UITableViewController that is attached to a TabBarController, and has a naviagation bar. However, I'm not sure what to do to send the message. Currently (for testing purposes), I have:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog( @"Selected tabbar");
    [viewController sendMessage];
}

NSLog fires off fine before the code fails on the next line with "unrecognized selector sent to instance...", which tells me I'm close, but I'm sending -sendMessage to the wrong object. What am a looking for to send -sendMessage to the UITableViewController instance?

A: 

I'm assuming that your UITableViewController instance has a method called -sendMessage. Remember that this will be called for every selection, so do all of your view controllers respond to -sendMessage? It seems surprising that you would want to send this in all cases. Perhaps you meant to include a line like:

if (viewController == self.tableViewController)

You can learn a lot about what's going on by adding something like this:

NSLog(@"Selected viewController: %@", viewController);

Then you'll know what object you're talking to (at least what class).

Rob Napier
I did check into that at one point, but all I can get out of it is <UINavigationController: 0xf47040>. I'm not sure how to use that to send a message to my UITableViewController. Any suggestions?
JoBu1324
Your UITableView appears to be nested inside of a UINavigationController, which is the view that is actually being selected by the tab bar controller. Why not just have your UITableViewController implement -viewDidAppear if there are things you want to perform when it appears?
Rob Napier
Because it's too easy ;) Seriously though, this was the first step in a larger problem, the larger problem being to notify a view controller when a different tab was selected (not when the view is dismissed).
JoBu1324
A: 

viewController has an array called viewControllers. In my case there is only one object in the array; It's just a guess, but I think objectAtIndex:0 is the topmost view controller.

Here's the code I was looking for:

activeViewController = [[viewController viewControllers] objectAtIndex:0];

It's a little off topic, but with this level of control, you can store the last active view controller, so that when you navigate away from a tab, you can send it any message you want; even ask it if it should stay active.

JoBu1324