从任何 class - iOS 访问标签栏徽章
Access tab bar badge from any class - iOS
我有一个基于标签栏视图控制器的应用程序。我在应用程序中有 3 个主视图控制器和一个类型为 NSObject
.
的自定义 class(称为 "DataManager")
由于我的 DataManager class 处理所有数据,我只想从 class(而不是视图控制器)更新标签栏徽章。
但是,我无法从我的自定义 class 访问标签栏视图控制器。有办法做到这一点吗?
这是我的代码(DataManger - header):
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DataManager : NSObject <UITabBarControllerDelegate> {
}
// Trolley data methods.
-(NSMutableArray *)get_trolley_data;
-(void)update_trolley:(NSMutableArray *)data;
-(int)get_trolley_size;
// Data check methods.
-(NSString *)text_check:(NSString *)data :(int)type;
// Tab bar methods.
-(void)update_badge;
@end
这里是实现代码:
#import "DataManager.h"
@implementation DataManager
/*
other methods.......
*/
/// TAB BAR METHODS ///
-(void)update_badge {
// Get the trolley size.
int num = [self get_trolley_size];
// Set the tab bar number badge.
UITabBarItem *tab_bar = [[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem];
// Show the badge if the count is
// greater than 0 otherwise hide it.
if (num > 0) {
[tab_bar setBadgeValue:[NSString stringWithFormat:@"%d", num]];
}
else {
[tab_bar setBadgeValue:nil];
}
return;
}
@end
我收到以下错误:
Property 'tabBarController' not found on object of type 'DataManager
*'.
那么有没有一种方法可以让我从我的自定义 class 访问标签栏控制器?如果我尝试从我的视图控制器更新标签栏徽章,那么我最终将多次复制上述方法,这很愚蠢。
谢谢你的时间,丹。
试试这个代码
-(void)update_badge {
// Get the trolley size.
int num = [self get_trolley_size];
// Get RootViewController That is surely your tabbarcontroller
UITabBarController *tabBarController =(UITabBarController*)[[(YourAppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController];
// Set the tab bar number badge.
UITabBarItem *tab_bar = [[tabBarController.viewControllers objectAtIndex:1] tabBarItem];
// Show the badge if the count is
// greater than 0 otherwise hide it.
if (num > 0) {
[tab_bar setBadgeValue:[NSString stringWithFormat:@"%d", num]];
}
else {
[tab_bar setBadgeValue:nil];
}
return;
}
注意: 必须将 YourAppDelegate
替换为您实际的 AppDelegate
class.
我有一个基于标签栏视图控制器的应用程序。我在应用程序中有 3 个主视图控制器和一个类型为 NSObject
.
由于我的 DataManager class 处理所有数据,我只想从 class(而不是视图控制器)更新标签栏徽章。
但是,我无法从我的自定义 class 访问标签栏视图控制器。有办法做到这一点吗?
这是我的代码(DataManger - header):
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DataManager : NSObject <UITabBarControllerDelegate> {
}
// Trolley data methods.
-(NSMutableArray *)get_trolley_data;
-(void)update_trolley:(NSMutableArray *)data;
-(int)get_trolley_size;
// Data check methods.
-(NSString *)text_check:(NSString *)data :(int)type;
// Tab bar methods.
-(void)update_badge;
@end
这里是实现代码:
#import "DataManager.h"
@implementation DataManager
/*
other methods.......
*/
/// TAB BAR METHODS ///
-(void)update_badge {
// Get the trolley size.
int num = [self get_trolley_size];
// Set the tab bar number badge.
UITabBarItem *tab_bar = [[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem];
// Show the badge if the count is
// greater than 0 otherwise hide it.
if (num > 0) {
[tab_bar setBadgeValue:[NSString stringWithFormat:@"%d", num]];
}
else {
[tab_bar setBadgeValue:nil];
}
return;
}
@end
我收到以下错误:
Property 'tabBarController' not found on object of type 'DataManager *'.
那么有没有一种方法可以让我从我的自定义 class 访问标签栏控制器?如果我尝试从我的视图控制器更新标签栏徽章,那么我最终将多次复制上述方法,这很愚蠢。
谢谢你的时间,丹。
试试这个代码
-(void)update_badge {
// Get the trolley size.
int num = [self get_trolley_size];
// Get RootViewController That is surely your tabbarcontroller
UITabBarController *tabBarController =(UITabBarController*)[[(YourAppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController];
// Set the tab bar number badge.
UITabBarItem *tab_bar = [[tabBarController.viewControllers objectAtIndex:1] tabBarItem];
// Show the badge if the count is
// greater than 0 otherwise hide it.
if (num > 0) {
[tab_bar setBadgeValue:[NSString stringWithFormat:@"%d", num]];
}
else {
[tab_bar setBadgeValue:nil];
}
return;
}
注意: 必须将 YourAppDelegate
替换为您实际的 AppDelegate
class.