Przemek Lach
Przemek Lach

Reputation: 1538

UITabBarController and UIViewController Transition

I have a UITabBarController that loads 2 UIViewControllers: a map and a table. When a tabBarItem is clicked, I catch the event and set some properties on the destination view:

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if([viewController.tabBarItem.title isEqualToString:@"Map"]) {
        MapViewController *MVC = [tabBarController.childViewControllers objectAtIndex: viewController.tabBarController.selectedIndex];
        MVC.billboards = self.billboards;
        MVC.rangeMaxWidth = self.rangeMaxWidth;
        MVC.rangeMaxHeight = self.rangeMaxHeight;
        MVC.APIRoot = self.APIRoot;
    }
}

Now the code above works great; however, when I try to do the same thing for my table view I get a cryptic error. Here is the full code:

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if([viewController.tabBarItem.title isEqualToString:@"Map"]) {
        MapViewController *MVC = [tabBarController.childViewControllers objectAtIndex: viewController.tabBarController.selectedIndex];
        MVC.billboards = self.billboards;
        MVC.rangeMaxWidth = self.rangeMaxWidth;
        MVC.rangeMaxHeight = self.rangeMaxHeight;
        MVC.APIRoot = self.APIRoot;

    } else if ([viewController.tabBarItem.title isEqualToString:@"Manage"]) {

        ManageBillboardsController *MBC = [tabBarController.childViewControllers objectAtIndex: viewController.tabBarController.selectedIndex];

        MBC.aNumber = [[NSNumber alloc] initWithInt:3];
    }
}

-[UINavigationController setANumber:]: unrecognized selector sent to instance 0x9e91400 2013-06-27 15:09:18.624 Billboard[3269:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setANumber:]: unrecognized selector sent to instance 0x9e91400'

I think I've double checked everything and everything seems to be the same for both views. So I don't understand why this works for the map view but not for the table.

Thanks in advance.

* SOLUTION *

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if([viewController.tabBarItem.title isEqualToString:@"Map"]) {
        MapViewController *MVC = [tabBarController.childViewControllers objectAtIndex: viewController.tabBarController.selectedIndex];
        MVC.billboards = self.billboards;
        MVC.rangeMaxWidth = self.rangeMaxWidth;
        MVC.rangeMaxHeight = self.rangeMaxHeight;
        MVC.APIRoot = self.APIRoot;

    } else if ([viewController.tabBarItem.title isEqualToString:@"Manage"]) {
       UINavigationController *NC = [tabBarController.childViewControllers objectAtIndex: viewController.tabBarController.selectedIndex];
       NSArray *controllers = [NC childViewControllers];

        for(id item in controllers){
            ManageBillboardsController *MBC = item;
            MBC.aNumber = [[NSNumber alloc] initWithInt:3];
        }
    }
}

Upvotes: 0

Views: 587

Answers (1)

Danilo
Danilo

Reputation: 3327

Seems like your table view is inside a UINavigationController. So MBC should be of type UINavigationController and then get your table view (if its on top) like so:

MBC.topViewController

Then set aNumber on it.

Upvotes: 1

Related Questions