5

我用

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[UIApplication sharedApplication].applicationIconBadgeNumber -1];

减少图标徽章计数,但实际上它被清除了。当我按下主页按钮时,徽章计数不存在。

但是,如果我尝试这样的固定值

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:4];

,徽章数量与预期一样。

所以,问题是:为什么

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[UIApplication sharedApplication].applicationIconBadgeNumber -1];

不工作?

4

5 回答 5

4

为什么你不只是:

NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber
numberOfBadges -=1;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
于 2013-03-04T07:52:34.473 回答
0

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[UIApplication sharedApplication].applicationIconBadgeNumber -1];是你的错

[UIApplication sharedApplication].applicationIconBadgeNumber -1

在那里没有有效的操作。

如果您对此感到困惑,请使用临时变量来更改徽章计数。

编辑:像这样使用它。

int value = [UIApplication sharedApplication].applicationIconBadgeNumber;
value--;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:value];
于 2013-03-04T08:01:03.467 回答
0

快速尝试以减少

    let currentCountStr = UIApplication.sharedApplication().applicationIconBadgeNumber.description
    let currentCount = Int(currentCountStr)
    if(currentCount > 0) {
        UIApplication.sharedApplication().applicationIconBadgeNumber = currentCount! - 1
    } else {
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }
于 2016-09-02T03:30:57.993 回答
-1

这不起作用的原因:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[UIApplication sharedApplication].applicationIconBadgeNumber -1];

是它没有减少徽章编号,而是将其设置为-1。文档说将徽章编号设置为 0 会隐藏它,但负数也会隐藏它。

要减少徽章编号,至少在它达到 0 之前,请执行以下操作:

[UIApplication sharedApplication].applicationIconBadgeNumber--;

applicationIconBadgeNumber 可以用作普通变量,例如:

[UIApplication sharedApplication].applicationIconBadgeNumber = 100;
[UIApplication sharedApplication].applicationIconBadgeNumber++;
[UIApplication sharedApplication].applicationIconBadgeNumber += 2;

如果您希望它可见,请确保它保持积极状态。

于 2013-10-29T00:13:30.107 回答
-2

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[dict[@"badge"] integerValue]]+=[UIApplication sharedApplication].applicationIconBadgeNumber;

于 2016-06-09T16:34:01.270 回答