我有一个带有ViewController的TabBar。我在AppDelegate中这样做。所以我有一个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];
现在我遇到了这行源代码的问题:
[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];
这里会有一个SIGBRT,因为selectedViewController在这种情况下是一个“UINavigationController”。但我想调用“Test2ViewController”的方法。我怎么能这样做? 通常我也会这样做:
if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }
但这也失败了,因为它是一个UINavigationController。如何解决?有谁知道吗?
提前多多感谢&最诚挚的问候。
答案 0 :(得分:2)
尝试以下方法:
UINavigationController *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
Test2ViewController *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];
[viewController methodName:arg1 withTag:arg2];