3

私はたくさん検索しましたが、答えを見つけることができなかったので、あなたに尋ねることにしました:)。

私はいくつかのビューを持つアプリケーションを持っています。ログイン後、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];
4

4 に答える 4

0

私は本当にあなたの問題を正確に知りませんが、nsobjectのサブクラスを使用し、各viewControllerでバッジを変更すると思います。これに似たもの: https://stackoverflow.com/a/9736559/2000162

于 2013-05-13T13:51:02.977 に答える
0

新しいクラスを作成するのではなく、クラスの同じインスタンスを使用する必要があります。以前の値を破壊します。クラスでバッジの void getter および setter メソッドを実装するバッジを取得したら、NSNotificationCenter を使用して通知を投稿することをお勧めしますplatformViewController。その場合、インスタンスは破棄されません。

お役に立てれば

于 2013-05-13T10:47:18.430 に答える