【发布时间】:2010-09-16 12:03:01
【问题描述】:
我在 appDelegate 中实例化了一个 UITabBarController,但我需要一种方法来了解用户何时按下不同的标签栏项目(标签栏上的按钮)。
-UITabBarDelegate 协议进行救援(使用所需的 didSelectViewController 方法)!
在 Interface Builder 中连接好所有东西后,我如何获得对对应于被按下的这个标签栏项目的实际 UIViewController 子类实例的引用?
我需要这个参考,因为每次按下标签栏项目时,我都需要在我的 UIViewControllers 子类之一中调用一个方法。
有什么建议吗?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
【问题讨论】:
-
看来您在激活的选项卡中使用
UINavigationController。如果是这样,您需要从UINavigationController获取顶视图控制器,并且您应该获取您的MySecondViewController实例。
标签: iphone uitabbarcontroller uitabbar uitabbaritem