0

I have a little problem. I want to set a Badge on a Tab after receiving push but I can't figure out how I can change the badge value (From my AppDelegate)

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
    {
        print("didReceiveRemoteNotification")
        //can't find or access the tabBarItem...

    }

I tried different things. One time I get a nil while unwrapping and sometimes I am creating a new TabBarController and I can change the badge Value... But the problem is that it isn't the TabBar which is shown. (First is nil and second is the "new" one)

Here some tries:

let tabBarC = self.storyboard?.instantiateViewControllerWithIdentifier("TC")
        let items = tabBarC?.tabBarController?.tabBar.items
        let tab = items![3]
        tab.badgeValue = "999"


let tabBarController: UITabBarController = storyboard!.instantiateViewControllerWithIdentifier("TC") as! UITabBarController

        let tabArray = tabBarController.tabBar.items as NSArray!
        let tabItem = tabArray.objectAtIndex(3) as! UITabBarItem
        tabItem.badgeValue = "!"
        print(tabItem.title)

Ah and my TabBarController isn't my rootViewController

Do you have any suggestions have I can fix this?

Thanks :)

Godlike
  • 1,621
  • 2
  • 20
  • 33

1 Answers1

0

Okay, I found a "solution"... I didn't find another solution which works for me...

I am setting a public variable BadgeValue. Every time I am receiving push I am changing it's value.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
    {
        BadgeValue = "!"
    }

In every VC on ViewDidAppear I do this:

BadgeValue = "" //Just in the Tab where the BadgeValue is shown
let tabArray = tabBarController?.tabBar.items as NSArray!
        let tabItem = tabArray.objectAtIndex(3) as! UITabBarItem
        if(BadgeValue != ""){
            tabItem.badgeValue = BadgeValue
        } else {
            tabBarItem.badgeValue = .None
        }

Maybe this helps you if you have the same problem... (I know that this solution isn't good but it works. Not perfect but it works)

Godlike
  • 1,621
  • 2
  • 20
  • 33