【发布时间】:2014-02-17 14:13:19
【问题描述】:
当有通知发送时,我想在我的应用程序内的 UIButton 中添加一个徽章图标,但我不太确定如何实现这一点。它需要匹配跳板图标上的徽章,并且当在应用中按下 UIButton 时,跳板徽章和 UIButton 上的徽章都需要消失。
更新:
我有一个徽章出现,但是当我打开应用程序时,徽章会在应用程序打开时重置为零,所以我最终看不到按钮上的徽章。我知道徽章有效,因为我通过这样做对其进行了测试:
[[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;
}
【问题讨论】:
-
我一直想知道这是否可能,我也需要这样做!
-
是的,这是可能的。正如@Viruss 回答的那样,您可以使用 UIApplication 的
applicationIconBadgeNumber属性来设置和获取徽章。
标签: ios objective-c uibutton push-notification badge