【问题标题】:Swift Tab Bar BadgeSwift 标签栏徽章
【发布时间】:2018-10-31 23:19:02
【问题描述】:

我有 2 个UIViewController

1- UIViewControllerUICollectionView。在单元格中,我有UIButtonUILabel。当我按下UIButton 时,我已将UILabel 中的文本保存到UserDefaults 数组中。 (主屏幕)

2-UIViewController

当我启动应用程序时,我想在单元格中点击 UIButton 并在第二个标签栏项目上显示带有 array.count 的徽章(例如,我点击 3 个不同的按钮和数组有 3 个计数)

我尝试通过 UICollectionViewCell 类中的协议来执行此操作,并使用 CollectionView 将扩展名添加到 UIViewController

【问题讨论】:

    标签: swift uitabbarcontroller uitabbar uitabbaritem badge


    【解决方案1】:

    didSelectItemAtIndexPath 的集合 ViewController 或您需要更改 secondViewcontroller Tabbar 徽章的任何代码部分

    就这样做

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    
     if let tabItems = self.tabBarController?.tabBar.items
            {
                // In this case we want to modify the badge number of the seond tab:
                let tabItem = tabItems[1]
                tabItem.badgeValue = "1" // set count you need
            }
    }
    

    【讨论】:

    • 你有两个视图控制器的标签栏?? ,分享你的代码,我在上面测试过代码,效果很好