从应用程序委托设置标签栏徽章
Setting tab bar badge from app delegate
我的 AppDelegate
中有一个观察器,我想用它在客户端收到新消息时在标签栏按钮上显示徽章编号。我可以使用此行 [[self navigationController] tabBarItem].badgeValue = @"1";
从 SampleViewController.m
的 viewDidLoad
编辑徽章,但不知道如何从 AppDelegate
设置它。我尝试添加 UITabBarDelegate
、导入 SampleViewController.h
并调用 [[SampleViewController navigationController] tabBarItem].badgeValue = @"1";
但没有成功。我还尝试在 SampleViewController
中实现一个 class 方法来更改徽章编号和来自观察者的调用,但无法将 [self navigationController] tabBarItem].badgeValue = @"1";
放入 class 方法中。有人可以帮助我实现这一目标吗?我知道我可以在每个 VC 中放置一个观察者,但从 AppDelegate 中这样做会更优雅。
AppDelegate.m
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
// DISPLAY A NUMBER IN THE TAB BAR BADGE WHEN THE CLIENT RECEIVES A MESSAGE
}
您可以使用 self.window.rootViewController
来访问您的视图层次结构的根视图控制器。到那时,就是遍历该层次结构以获取对所需视图控制器的引用。
您可以通过简单的实现来子class UITabBarController。然后你仍然可以 post 一个 NSNotifcation,但只需要一个 class 来观察它。我只是 运行 这个测试:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self performSelector:@selector(notify) withObject:nil afterDelay:5.0];
return YES;
}
-(void)notify{
[[NSNotificationCenter defaultCenter]postNotificationName:@"post" object:nil];
}
//additional implementation...
以及您的自定义 TabController
@implementation TabController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(notify) name:@"post" object:nil];
}
-(void)notify{
UITabBarItem *item = self.tabBar.items[0];
item.badgeValue = @"1";
}
@end
显然,不要在您自己的 AppDelegate 中使用 performSelector。我这样做只是为了启动我的测试应用程序,验证选项卡栏上没有徽章,然后在 5 秒后观察它自行更新。您应该将通知放在适当的位置。
我的 AppDelegate
中有一个观察器,我想用它在客户端收到新消息时在标签栏按钮上显示徽章编号。我可以使用此行 [[self navigationController] tabBarItem].badgeValue = @"1";
从 SampleViewController.m
的 viewDidLoad
编辑徽章,但不知道如何从 AppDelegate
设置它。我尝试添加 UITabBarDelegate
、导入 SampleViewController.h
并调用 [[SampleViewController navigationController] tabBarItem].badgeValue = @"1";
但没有成功。我还尝试在 SampleViewController
中实现一个 class 方法来更改徽章编号和来自观察者的调用,但无法将 [self navigationController] tabBarItem].badgeValue = @"1";
放入 class 方法中。有人可以帮助我实现这一目标吗?我知道我可以在每个 VC 中放置一个观察者,但从 AppDelegate 中这样做会更优雅。
AppDelegate.m
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
// DISPLAY A NUMBER IN THE TAB BAR BADGE WHEN THE CLIENT RECEIVES A MESSAGE
}
您可以使用 self.window.rootViewController
来访问您的视图层次结构的根视图控制器。到那时,就是遍历该层次结构以获取对所需视图控制器的引用。
您可以通过简单的实现来子class UITabBarController。然后你仍然可以 post 一个 NSNotifcation,但只需要一个 class 来观察它。我只是 运行 这个测试:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self performSelector:@selector(notify) withObject:nil afterDelay:5.0];
return YES;
}
-(void)notify{
[[NSNotificationCenter defaultCenter]postNotificationName:@"post" object:nil];
}
//additional implementation...
以及您的自定义 TabController
@implementation TabController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(notify) name:@"post" object:nil];
}
-(void)notify{
UITabBarItem *item = self.tabBar.items[0];
item.badgeValue = @"1";
}
@end
显然,不要在您自己的 AppDelegate 中使用 performSelector。我这样做只是为了启动我的测试应用程序,验证选项卡栏上没有徽章,然后在 5 秒后观察它自行更新。您应该将通知放在适当的位置。