我有2个UIViewController
1- UIViewController
和 UICollectionView
。在单元格中,我有 UIButton
和 UILabel
。当我按下 UIButton
时,我已将文本从 UILabel
保存到 UserDefaults 数组中。 (主屏幕)
2- UIViewController
当我启动应用程序时,我想点击单元格中的 UIButton
并在第二个选项卡栏上显示带有 array.count 的徽章(例如,我点击 3 个不同的按钮,数组有 3 个计数)项目
我尝试通过 UICollectionViewCell
类中的协议(protocol)来执行此操作,并使用 CollectionView 将扩展添加到 UIViewController
最佳答案
在 didSelectItemAtIndexPath
集合 ViewController 中或需要更改 secondViewcontroller
标签栏徽章的任何代码部分
就这样做
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
}
}
关于Swift 标签栏徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50463438/