如何在更多部分获得UITabBarItem的标题?

时间:2010-05-23 22:36:28

标签: iphone objective-c cocoa-touch xcode uitabbarcontroller

我有UITabBarControllerDelegate方法确定UITabBarItem的标题并做相应的事情。这适用于UITabBar中的项目,但当我点击“更多”按钮时,UITabBarItems的其余部分位于UITableView中。如何在“更多”部分中确定标题?

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    if ([self.tabBarController.selectedViewController.title isEqualToString:@"All"]) {
        //do something
    }
}

1 个答案:

答案 0 :(得分:10)

无论何时在UITabBarController中选择视图控制器,都会调用您提到的方法,最重要的是,当前显示的视图控制器将作为参数传递给您;然后,您可以使用以下代码查找控制器的类和标题,包括“更多”控制器:

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);
}

在快速测试中,只需在Xcode中添加几个控制器,这就是你在控制台中得到的:

2011-03-28 09:13:21.795 TabTest[39015:207] controller class: UIViewController
2011-03-28 09:13:21.797 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:23.922 TabTest[39015:207] controller class: UITableViewController
2011-03-28 09:13:23.925 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:24.505 TabTest[39015:207] controller class: UITableViewController
2011-03-28 09:13:24.506 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:24.945 TabTest[39015:207] controller class: UIMoreNavigationController
2011-03-28 09:13:24.945 TabTest[39015:207] controller title: More

另一方面,当你选择里面的控制器“更多”列表时,你的UITabBarControllerDelegate方法就不会得到通知(很奇怪,恕我直言)。为了帮助您在该列表中选择控制器时收到通知,您可以执行以下操作:

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController == self.tabBarController.moreNavigationController)
    {
        NSLog(@"more controller class: %@", NSStringFromClass([viewController class]));
        NSLog(@"more controller title: %@", viewController.title);
    }
}

当然,你的类也应该实现UINavigationControllerDelegate协议。

这是样本运行的结果,使用上面的代码并在UITabBar和“更多”列表中点击几次:

2011-03-28 09:27:42.496 TabTest[39113:207] controller class: UIViewController
2011-03-28 09:27:42.498 TabTest[39113:207] controller title: (null)
2011-03-28 09:27:44.306 TabTest[39113:207] controller class: UIMoreNavigationController
2011-03-28 09:27:44.307 TabTest[39113:207] controller title: More
2011-03-28 09:27:44.310 TabTest[39113:207] more controller class: UIMoreListController
2011-03-28 09:27:44.311 TabTest[39113:207] more controller title: More
2011-03-28 09:27:45.632 TabTest[39113:207] more controller class: SecondViewController
2011-03-28 09:27:45.634 TabTest[39113:207] more controller title: (null)
2011-03-28 09:27:47.156 TabTest[39113:207] more controller class: UIMoreListController
2011-03-28 09:27:47.156 TabTest[39113:207] more controller title: More
2011-03-28 09:27:48.581 TabTest[39113:207] controller class: UITableViewController
2011-03-28 09:27:48.582 TabTest[39113:207] controller title: (null)

希望这有帮助!