2013-04-11 34 views
-2

我正在制作一个年龄计算应用程序,并且我想将应用程序内部标签的天数连接到应用程序徽章(应用程序图标上的小红色形状,例如邮件(当您有邮件))。 有没有办法做到这一点?如何将应用图标徽章连接至应用中的标签?

所有帮助表示赞赏!

[UIApplication sharedApplication].applicationIconBadgeNumber = 42; //Number of Days 

例如,如果你的标签说只是一个数字,你可以使用此代码:

NSInteger number = self.myLabel.text.integerValue; 
[UIApplication sharedApplication].iconBadgeNumber = number; 
+0

@ H2CO3是的:)我投你向上。尽管如此,代码仍然可以在'setMyLabelText:'中进行。 – Undo 2013-04-11 20:35:20

回答

1

的应用程序图标徽章可以通过设置:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:YOUR_VALUE]; 

改变对徽章

图标的价值
0
NSInteger badgeNumber = [[yourLabel text] integerValue]; 
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeNumber]; 
1

如果我理解正确的话,你要同步与图标徽章标签值的值:

// Add an observer that listens for changes in the text of the label 
[label addObserver:self 
     forKeyPath:@"text" 
      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld 
      context:NULL]; 

// Implement the observer method on `self` 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    NSString *text = [change objectForKey:NSKeyValueChangeNewKey]; 
    [UIApplication sharedApplication].applicationIconBadgeNumber = text.integerValue; 
} 
相关问题