2016-07-27 1 views
-1

iは目的を使ってiOSアプリケーションを開発しています。私のアプリでは、ユーザーが登録されていない場合、レジスタビューコントローラはrootViewControllerです。ユーザーが登録している場合、tabBarControllerは3つのタブを持つrootViewControllerです。どのビューコントローラからもtabBarItemバッジ値を設定する必要があります。私が3番目のタブにあり、それが別のView ControllerとSegueを持っていて、私がそのView Controller上にいるとすれば、ここから最初のView ControllerのtabBarItemバッジ値を変更する必要があります。私はviewwillappearでiOS tabBarItem任意のビューからのバッジ値の変更

NSString *upcomingcount=[NSString stringWithFormat:@"%lu",(unsigned long)self.arrbadge.count]; 
      self.navigationController.tabBarItem.badgeValue=upcomingcount; 

を使用していて、私の場合にはtabBarItemバッジ値更新のみ私はそのタブに移動します。

badgeValueを任意のViewControllerから設定する方法はありますか? 0からのバッジ値を更新したいViewController

答えて

1

plzは働いていない

@property (nonatomic,strong) AppDelegate *appDelegate; 




self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    [self.appDelegate update_badgeWithViewControllerIndex:yourViewControllerIndex]; 
+0

を作成するすべてのViewControllerにこの方法 アプリデリゲートに使用するために

- (void)update_badgeWithViewControllerIndex:(NSInteger)ViewControllerIndex { UITabBarController *tabBarController =(UITabBarController*)[[(AppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController]; // Set the tab bar number badge. UITabBarItem *tab_bar = [[tabBarController.viewControllers objectAtIndex:ViewControllerIndex] tabBarItem]; // Show the badge if the count is // greater than 0 otherwise hide it. if ([badgeValue > 0) { [tab_bar setBadgeValue:badgeValue]; // set your badge value } else { [tab_bar setBadgeValue:nil]; } } 

を、このメソッドを使用!バッジの更新は、そのビューコントローラに行った場合にのみ行われます。応答のためにありがとう – goks

+0

実際に私はまた、他の方法ラウンドを試みた。作成したView ControllerのプロトコルとそのメソッドをAppDelegate.mに委譲し、現在のView Controllerの.hファイルをAppDelegate.hにインクルードしてプロトコルを宣言し、xcodeでプロトコルの宣言が見つかりません。 .... "" "。" – goks

+0

あなたの答えから考えていただきありがとうございました...現在作業中です – goks

0

upcomingcountの変更をKVOで観察し、バッジ値を更新することができます。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ 

     if ([keyPath isEqualToString:NSStringFromSelector(@selector(upcomingcount))]) { 
      //SET BADGE VALUE HERE 

     } 

} 
関連する問題