查看收到的通知后,将徽章值重置为0 [英] Resetting the badge value back to 0 after viewing the received notification

查看:69
本文介绍了查看收到的通知后,将徽章值重置为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我在收到通知时尝试增加徽章值时被困住了。我有一个观察者设置为每次收到通知时增加指定的徽章值。它非常好用:

I've been stuck for the past few days trying to increment a badge value when it receives a notification. I have an observer set up to increment the specified badge value each time a notification is received. It works perfectly fine:

//adds the observer. if it goes off then add one to the notification badge
    NotificationCenter.default.addObserver(forName: PRIVATE_NOTIFICATION, object: nil, queue: nil) { (notification) in
        //increment a variable
        self.privateBadge += 1

        if let notificationTab = self.tabBar.items?[1]{
            if self.tabBar.selectedItem != notificationTab as UITabBarItem {
        notificationTab.badgeValue = "\(self.privateBadge)"
        if #available(iOS 10.0, *) {
            notificationTab.badgeColor = ChatMessageCell.indexedColor
        } else {
            // Fallback on earlier versions
        }
        }}
    }

我的问题是尝试在删除观察者后重置变量(privateBadge)。
在获取通知标记的选项卡上的 viewDidAppear 中,我设置了一个方法,用于在用户点击该标签后将徽章值重置为0: / p>

My issue is trying to get the variable (privateBadge) to reset once the observer is removed. In viewDidAppear on the tab that gets a notification badge, i have a method set up to reset the badge value back to 0 after the user taps that tab:

//View Will Appear
override func viewWillAppear(_ animated: Bool) {
    //Removes the badgeValue and resets messageBadge to 0 in an attempt to start the loop over.
    if let thisTab = self.tabBarController?.tabBar.items?[1]{
        NotificationCenter.default.removeObserver(self, name: PRIVATE_NOTIFICATION, object: nil)
        thisTab.badgeValue = nil
        tabBarControllerClass.privateBadge = 0
    }
}

badgeValue尽可能清除,但我的问题是当我收到另一个通知时会发生什么。它不是从0开始并递增到1,而是从它停止的地方开始。因此,例如,如果我收到2个通知,则badgeValue将等于2.如果我使用badgeValue转到该选项卡,它将清除并应重置为0,但事实并非如此。相反,下次我收到通知时,它会说3而不是1.

the badgeValue clears as it should but my issue is what happens when I receive another notification. instead of starting over at 0 and incrementing to 1, it picks up where it left off. So for instance, if I receive 2 notifications, the badgeValue will be equal to 2. If I go to that tab with the badgeValue, it clears and should reset back to 0, but it doesn't. Instead, the next time I get a notification, it will say 3 instead of 1.

有没有更好的解决方案?我应该设置一个协议/代理系统吗?

Are there any better solutions? Should I set up a protocol/delegate system instead?

推荐答案

这看起来像你的坏孩子,

this looks like your bad boy,

self.privateBadge += 1



<你可能忘了把它设置回0,

you probably forgot to set it back to 0,

thisTab.badgeValue = nil
self.privateBadge = 0

不确定你的情况是否可行,因为我不知道它是否是一样的class。

Not sure if this is possible in your case as I don't see if it is the very same class.

这篇关于查看收到的通知后,将徽章值重置为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆