swift - 在 appDelegate 中设置 UITabBarItem 徽章值

标签 swift appdelegate uitabbaritem

我对这个问题进行了大量搜索,并找到了许多在 Objective-C 中完成此任务的答案。但是,我还没有找到 Swift 中的答案。

我尝试翻译 Objective-C,在 didFinishLaunchingWithOptions 中执行以下代码:

if let rootViewController = self.window?.rootViewController {
    print("root")
    if let tabBarController = rootViewController.tabBarController {
        print("tab")
        let tabBarItem = tabBarController.tabBar.items![3]
        tabBarItem.badgeValue = "!"
    }
}

代码从不打印“制表符”,所以我显然没有正确访问它。帮忙?

最佳答案

继续假设您的 Root View Controller 实际上是标签栏 Controller ,您需要更改:

if let tabBarController = rootViewController.tabBarController {

到:

if let tabBarController = rootViewController as? UITabBarController {

关于swift - 在 appDelegate 中设置 UITabBarItem 徽章值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341883/

相关文章:

ios - 如何从分段控件中删除边框

swift - 为什么 "Expected to decode Data but found an array instead"我的代码中没有数组

ios - 收到推送通知时使用协议(protocol)更新 UIViewController?

iphone - 如何去除 UITabbaritem 图像的渐变效果和投影

ios - Firebase 实时数据库分页,一次显示 20 条记录

ios - 如何将 Objective-C App Delegate 转换为 Swift?

ios - 从 viewcontroller 获取 appdelegate 值

ios - TabBar 图标对选择的弹跳效果,就像 Swift 中的 Twitter 应用程序

ios - 在 viewDidLoad() 中以编程方式更改 Tabbar 项目标题

iOS - WatchKit 如何将消息/数据从 iPhone 应用程序发送到 WatchKit 应用程序?