如何更改未加载的视图控制器的徽章?

How to change the badge of a view controller that isn't loaded?

我有一个标签栏控制器,它附有 3 个视图控制器。

假设您启动应用程序,第一个视图控制器是其他三个视图中唯一加载的视图。我想设置未加载的第三个视图控制器的标志值。这是因为我希望用户在打开应用程序时知道他们有通知。

这可能吗?我该怎么做?我尝试了以下方法:

从另一个 class:

调用视图(我想更改其徽章的视图)
 NotificationsViewController *class = [NotificationsViewController alloc]init]; 

[class view];

然后,在 NotificationsViewController class 中,如果应用有任何使用解析的通知,我将标记设置为 1:

- (void)viewDidLoad {
[super viewDidLoad];

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge > 0) {

    NSString *badge = @"1";

        [self.tabBarItem setBadgeValue:badge];


    }
   }

为什么这不起作用?如何更改尚未加载的视图控制器的标志?

首先,您需要获取标签栏项目:

UITabBarItem *tabBarItem = [[self.tabBarController.viewControllers objectAtIndex:YOUR_INDEX] tabBarItem];

然后,简单地设置徽章就可以了。

NSString *badge = @"1";
[tabBarItem setBadgeValue:badge];