【问题标题】:set badge on tabbar item when notification received收到通知时在标签栏项目上设置徽章
【发布时间】:2015-01-03 22:15:35
【问题描述】:

收到推送通知时,我尝试为UITabBarItem 设置badgeValue。我正在使用此代码。这里UITabBarController 不是rootViewController。我在 resign active 方法中尝试了同样的事情,但它也不起作用。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

  UITabBarController *tabBarController = (UITabBarController *)[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"tabBarController"] ;
  [[tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:@"1"];

}

【问题讨论】:

  • 你需要学习对象管理和创建。您正在尝试在您从情节提要中实例化的新 UITabBarControllerUITabBarItem 上设置徽章。也请阅读NSNotification。它将帮助您了解如何实现它。

标签: ios push-notification xcode5 uitabbarcontroller badge


【解决方案1】:

我认为您可以在收到 remoteNotification 时使用 NSNotificationCenter 发布通知

在您的 UITabBarController 初始化方法中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationReceived:) name:@"pushNotification" object:nil];

并且 在myNotificationReceived:

[[self.tabBar.items objectAtIndex:2] setBadgeValue:@"1"];

当您收到远程通知时

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

这样就可以获得完整的RemoteNotification信息

【讨论】:

  • 当应用程序处于前台状态时,用户可以在任何屏幕,使用通知中心,我需要为所有屏幕添加观察者,我可以在没有观察者的情况下实现这一点。
  • 你只需将观察者添加到UITabbarController,你可以使用self.viewControllers访问UITabbarController中的任何viewcontroller。这将返回UITabbarController中的viewController数组。
  • 谢谢,我应该继承 uiTabBarController 来接收通知吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2013-12-01
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多