【问题标题】:Access to tabbar Controller from AppDelegate从 AppDelegate 访问标签栏控制器
【发布时间】:2014-01-06 10:02:27
【问题描述】:

我有这种情况:

如何从AppDelegate 访问第三个标签栏徽章?

我用过这段代码:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *vc = [[UIViewController alloc] init];
    if ([nonLette isEqualToString:@"0"]) {
        for (vc in tabBarController.viewControllers) {
            if (vc.tabBarItem.tag == 999) {
                vc.tabBarItem.badgeValue = nil;
            }
        }

但self.window.rootViewController 返回“开始视图”,因此不起作用。

【问题讨论】:

    标签: uitabbar appdelegate badge


    【解决方案1】:

    好的,我解决了,但部分解决了!!!

    这种方法效果很好。通过调试,我可以看到现在我可以更改所需选项卡上的徽章,但实际上不要更改图形(红色数字未出现)。

    有什么建议吗?

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UITabBarController *myTabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"];
        if ([nonLette isEqualToString:@"0"]) {
            for (UIViewController *vc in myTabBarController.viewControllers) {
                if (vc.tabBarItem.tag == 999) {
                    vc.tabBarItem.badgeValue = nil;
                }
            }
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        } else {
            for (UIViewController *vc in myTabBarController.viewControllers) {
                if (vc.tabBarItem.tag == 999) {
                    vc.tabBarItem.badgeValue = nonLette;
                }
            }
            [UIApplication sharedApplication].applicationIconBadgeNumber=[result integerValue];
        }
    

    【讨论】:

      【解决方案2】:
      let storyboard = UIStoryboard(name: "Main", bundle: nil)
                  let tabController = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController
      
                  let tabArray = tabController.tabBar.items as NSArray!
                  let alertTabItem = tabArray?.object(at: 3) as! UITabBarItem
      
                  alertTabItem.badgeValue = "10"
      

      【讨论】:

      • 到处寻找这个答案!伟大的工作:),我可以确认这可以很好地从情节提要中获取访问权限。只需记住在界面生成器中设置标识符以便它工作。确认这在 Swift 5 中有效
      • 我做了这样的事情:let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController,让 viewControllers = tabBarController.viewControllers else { return true }
      【解决方案3】:

      使用新的 XCode 11 和 Swift 5,这不再可能。

      相反,您想在 SceneDelegate.swift 中执行此操作,尤其是此函数 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

      然后你想做这样的事情。

      self.window = self.window ?? UIWindow()//@JA- If this scene's self.window is nil then set a new UIWindow object to it.
      
              //@Grab the storyboard and ensure that the tab bar controller is reinstantiated with the details below.
              let storyboard = UIStoryboard(name: "Main", bundle: nil)
              let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
      
      self.window!.rootViewController = tabBarController //Set the rootViewController to our modified version with the StateController instances
              self.window!.makeKeyAndVisible()
      
              print("Finished scene setting code")
              guard let _ = (scene as? UIWindowScene) else { return }
      

      希望这对使用较新 XCode 的人们有所帮助。这里的关键是 appDelegate 不再可以访问窗口,sceneDelegate 可以。 如果您尝试使用 appDelegate,您将收到窗口不再存在的错误。

      确保为情节提要中的 tabBarController 设置了 标识符,以便它可以找到它。您可以随意命名,但只需在此处的代码中为 withIdentifier 函数引用该名称

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        • 2018-12-25
        相关资源
        最近更新 更多