iOS,减少图标徽章编号

时间:2013-03-04 07:42:43

标签: ios icons badge

我用

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

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

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:4];

,徽章数量符合预期。

所以,问题是:为什么

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

不工作?

5 个答案:

答案 0 :(得分:4)

为什么你不只是:

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

答案 1 :(得分:0)

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

[UIApplication sharedApplication].applicationIconBadgeNumber -1

没有有效的操作。

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

编辑: 像这样使用它。

int value = [UIApplication sharedApplication].applicationIconBadgeNumber;
value--;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:value];

答案 2 :(得分:0)

尝试使用swift减少

    let currentCountStr = UIApplication.sharedApplication().applicationIconBadgeNumber.description
    let currentCount = Int(currentCountStr)
    if(currentCount > 0) {
        UIApplication.sharedApplication().applicationIconBadgeNumber = currentCount! - 1
    } else {
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

答案 3 :(得分:-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;

如果您希望它可见,请确保它保持正面。

答案 4 :(得分:-2)

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