如何在用户单击另一个选项卡后删除 UITabBar 徽章?

发布于 2024-09-13 13:28:30 字数 283 浏览 5 评论 0原文

我想在用户单击另一个选项卡后立即删除徽章。我正在尝试这样做:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
    tbi.badgeValue = nil;
}

但它不起作用。

I want to remove a badge as soon as the user clicks another tab. I am trying to do:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
    tbi.badgeValue = nil;
}

But it doesn't work.

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

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

发布评论

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

评论(1

千秋岁 2024-09-20 13:28:30

您想从当前选项卡或触摸的选项卡中删除徽章吗?

无论哪种方式,执行此操作的正确位置是在选项卡栏控制器委托中:

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

请注意,每当用户点击选项卡栏按钮时都会调用此函数,无论显示的新视图控制器是否与旧视图控制器不同,因此您需要跟踪当前的可见视图控制器。您也可以在此处更新:

- (void)tabBarController:(UITabBarController *)tabBarController 
        didSelectViewController:(UIViewController *)viewController {
    if(viewController != self.currentTabVC) {
        // if you want to remove the badge from the current tab
        self.currentTabVC.tabBarItem.badgeValue = nil;

        // or from the new tab
        viewController.tabBarItem.badgeValue = nil;

        // update our tab-tracking
        self.currentTabVC = viewController;
    }
}

You want to remove a badge from the current tab, or the one touched?

The right place to do this, either way, is in your tab bar controller delegate, in:

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

Note that this function gets called whenever the user taps on a tab bar button, regardless of whether the new view controller shown is different from the old one, so you'll want to track your current visible view controller. This is where you'll update that, too:

- (void)tabBarController:(UITabBarController *)tabBarController 
        didSelectViewController:(UIViewController *)viewController {
    if(viewController != self.currentTabVC) {
        // if you want to remove the badge from the current tab
        self.currentTabVC.tabBarItem.badgeValue = nil;

        // or from the new tab
        viewController.tabBarItem.badgeValue = nil;

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