1

I've done a bunch of searching on this question, and found numerous answers to accomplish this in Objective-C. However, I've yet to find an answer that is in Swift.

I've tried translating the Objective-C, with the following code executed in didFinishLaunchingWithOptions:

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

The code never prints "tab", so I'm obviously not accessing it correctly. Help?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alex Ritter
  • 1,009
  • 3
  • 18
  • 40

1 Answers1

1

Going on the assumption that your root view controller is actually the tab bar controller, you need to change:

if let tabBarController = rootViewController.tabBarController {

to:

if let tabBarController = rootViewController as? UITabBarController {
rmaddy
  • 314,917
  • 42
  • 532
  • 579