我想在发送通知时向应用程序中的UIButton添加徽章图标,但我不太确定如何实现此目的。它需要与跳板图标上的徽章相匹配,并且在应用程序中按下UIButton时,跳板徽章和UIButton上的徽章都需要消失。将推送通知徽章添加到UIButton
UPDATE:
我得到一个徽章出现,但是当我打开应用程序的徽章被重置回零的应用程序被打开时,所以我最终没有看到按钮上的徽章。我知道徽章的作品,因为我做这个测试吧:
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:1];
下面是徽章代码:
NSString *badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];
JSCustomBadge *actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
actionAlertBadge.frame = CGRectMake(188, 6, 30, 30);
[self.view addSubview:actionAlertBadge];
if ([badgeNumber isEqual:@"0"])
{
actionAlertBadge.hidden = YES;
}
我怎么能有徽章得不到打开应用程序时重置,但而是在应用程序中按下按钮时重置?
我有这个工作,但仍然有一个问题。如果应用程序未在后台运行,则徽章会显示在跳板图标和UIButton上。当按钮被按下时,新的视图打开并且徽章被重置,所以当用户用UIButton返回到屏幕时,徽章不再存在。我遇到的问题是应用程序在后台运行时,徽章不会显示在按钮上,而是显示在跳板上。
- (IBAction)gotoActionAlerts
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[[UAPush shared] resetBadge];
actionAlertBadge.hidden = YES;
[self.navigationController pushViewController:actionAlerts animated:YES];
}
在viewDidLoad中
badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];
actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);
[self.view addSubview:actionAlertBadge];
if ([badgeNumber isEqualToString:@"0"])
{
actionAlertBadge.hidden = YES;
}
我一直在想这是否可能,我需要做同样的事情! – meda
是的,这是可能的。作为@Viruss的回答,你可以使用UIApplication的'applicationIconBadgeNumber'属性来设置和获取徽章。 –