views:

210

answers:

1

Hello!

I have a TabBar with ViewController in it. I do this in my AppDelegate. So I have one UINavigationController

test1ViewController = [[Test1ViewController alloc] init];
test2ViewController = [[Test2ViewController alloc] init];
test3ViewController = [[Test3ViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: test2ViewController];

NSArray* controllers = [NSArray arrayWithObjects: test1ViewController, navigationController, test3ViewController, nil];
[self.tabBarController setViewControllers:controllers animated:YES];

[navigationController release];

Now I have the problem with this line of source code:

[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];

Here there will be a SIGBRT, because the selectedViewController is in this case an "UINavigationController". But I want to call a method of the "Test2ViewController". How could I do this? Normally I also do this:

if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }

But this also fail because it is a UINavigationController. How to fix that? Does anyone know?

Thanks a lot in advance & Best Regards.

+1  A: 

Try the following:

UINavigationController   *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
Test2ViewController      *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];

[viewController methodName:arg1 withTag:arg2];
Ben Gottlieb
does not work:Warning: 'UINavigationController' may not respond to '-rootViewController'.Also in this line SIGABRT occur.
Tim
sorry, try modified code: [[navController viewControllers] objectAtIndex: 0];
Ben Gottlieb
that works, but could I do it with one line:[[[[appDelegate.myTabBarController selectedViewController] viewControllers] objectAtIndex: 0] methodName:arg1 withTag:arg2];This works, but a Warning appears:'UIViewController' may not respond to '-viewControllers'Is it possible to avoid this warning message?Thanks a lot!
Tim
We're not trying to send a UIViewController the viewControllers message, we're trying to send that to a UINavigationController; check your parentheses and brackets.
Ben Gottlieb
Oh yeah, I see my mistake. Thanks a lot!
Tim