2013-04-11 54 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; 
} 
相關問題