如何获取“更多”部分中 UITabBarItem 的标题?

发布于 2024-09-02 10:32:52 字数 498 浏览 2 评论 0原文

我有一个 UITabBarControllerDelegate 方法,它确定 UITabBarItem 的标题并执行相应的操作。这对于我的 UITabBar 中的项目效果很好,但是当我单击“更多”按钮时,我的 UITabBarItems 的其余部分位于 UITableView 中。如何确定“更多”部分中的标题?

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

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

I have a UITabBarControllerDelegate method that determines the title of the UITabBarItem and does something accordingly. This works well for items in my UITabBar but when I click on the More button the rest of my UITabBarItems are in a UITableView. How can I determine the title in the More section?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-09-09 10:32:52

每当您在 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)

希望这有帮助!

Whenever you select a view controller in your UITabBarController, the method you mention will be called, and most important, the view controller currently shown will be passed to you as parameter; you can then use the following code to find the class and title of the controller, including the "more" controller:

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

In a quick test, just by adding a couple of controllers in Xcode, this is what you get in the console:

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

On the other side, when you select a controller inside the "more" list, you won't be notified in your UITabBarControllerDelegate method (weird, IMHO). To help you get notifications when you select controllers in that list, you could do the following:

- (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);
    }
}

Your class should also implement the UINavigationControllerDelegate protocol, of course.

This is the result of a sample run, using the above code and tapping a couple of times in the UITabBar and the "more" list:

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)

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文