私はたくさん検索しましたが、答えを見つけることができなかったので、あなたに尋ねることにしました:)。
私はいくつかのビューを持つアプリケーションを持っています。ログイン後、3 つのタブを持つ UITabBarController を作成します。
ここで、ユーザーが持っている通知の数に基づいて、2 番目のタブのバッジを変更したいと考えています。
このコアは、viewdidload メソッドで呼び出されると機能します。
NSString *badgeValue = [NSString stringWithFormat:@"%i", [cacheNotifications count]];
if([cacheNotifications count] != 0){
[[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:badgeValue];
}
ただし、30 秒ごとに通知をチェックするデーモンをバックグラウンドで実行しています。このデーモンからバッジを変更できれば最高です。
このようなものを呼び出すと:
PlatformViewController *theInstance = [[PlatformViewController alloc] init];
[theInstance updateNotificationsBadge];
関数を呼び出しますが、バッジを更新しません。performSelectorOnMainThread の有無にかかわらず。
updateNotificationsBadge:
-(void) updateNotificationsBadge{
NSString *badgeValue = [NSString stringWithFormat:@"%i", [cacheNotifications count]];
NSLog(@"Length here is: %i", [cacheNotifications count]);
if([cacheNotifications count] > 0){
NSLog(@"call..");
[self performSelectorOnMainThread:@selector(setNotification:)
withObject:badgeValue
waitUntilDone:YES];
}
}
-(void)setNotification:(NSString*)badge{
NSLog(@"Called. %@", badge);
[[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:badge];
}
どうすればこれを修正できますか?
前もって感謝します!
編集:
cacheNotifications は、globalVars.m で宣言されたグローバル変数です。新しいインスタンスを呼び出しても再初期化されません。
以下のコードを daemonClass.m から呼び出します
PlatformViewController *theInstance = [[PlatformViewController alloc] init];
[theInstance updateNotificationsBadge];