【问题标题】:Best way to update badgeValue of UITabBarController from a UIView从 UIView 更新 UITabBarController 的 badgeValue 的最佳方法
【发布时间】:2010-05-18 03:18:33
【问题描述】:

我在 AppDelegate 中设置了一个 tabBarController,并有一些带有导航控制器的 UIViewController。在其中一个 TabBar 项目中,在我推送了几个 UIView 之后,我想更新另一个 TabBar 项目的 badgeValue 项目。

最好的方法是什么?我真正能想到的唯一方法是 NSNotification 和值的单例存储,但对于一些简单的事情似乎需要做很多工作,而且我不知道 NSNotifications。

我对 super.tabBarController.otherView.tabBarItem.badgeValue = @"1" 之类的东西有一个疯狂的猜测(因为我以类似的方式设置了选择哪个选项卡),但我并不惊讶这不起作用。

谢谢

【问题讨论】:

    标签: iphone uiview uitabbarcontroller nsnotifications


    【解决方案1】:

    感谢 alku83 为我指明了正确的方向,代码是:

    [[super.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = @"1";
    

    【讨论】:

      【解决方案2】:

      我将 Xcode 4.5 与 Storyboards 和 iOS 6 一起使用,因此自最初发布以来,答案可能已经改变。

      首先你必须像这样访问标签栏控制器:

      UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
      

      然后你可以像这样设置徽章:

      [[tabController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"New!";
      

      【讨论】:

      • 这基本上是一样的,不同的是你是通过窗口单例访问UITabBarController,接受的答案是通过当前控制器的父级访问它。如果UITabBarController 是窗口上的根视图控制器,您的将起作用,否则它可能会崩溃。接受的答案将适用于直接孩子,但如果您尝试从控制器更新几层,则会变得混乱。
      【解决方案3】:

      我现在手头没有代码,但它应该更像

      ...otherViewController.tabBarItem.badgeValue = 1;
      

      【讨论】:

      • 没错,我已经改变了我原来的问题,但没有帮助解决它:(
      • 确保将徽章设置为整数值,而不是 NSString 值。如果这仍然不起作用,您可以为 TabBarItem 创建一个 IBOutlet 并以这种方式引用它。
      • 我实际上已经在 AppDelegate 中设置了它,以确保它可以工作并且它接受一个字符串。我对 IBOutlet 不太确定,因为我通过代码将所有内容添加到笔尖,IBOutlet 只有在笔尖中设置时才能工作
      • 迭代 TabBarController 的 viewController 怎么样?我会直接访问源代码(例如 AppDelegate),而不是 super.tabBarController。尽管如此,所有这一切似乎有点像一种解决方法,您是否检查过被引用的 tabBarItem 是否实际上不是 nil ?
      • 在同一个代码区域我有 [super.tabBarController setSelectedIndex:2] 并且工作正常。我认为它可能通过这样的方式,因为 badgeValue 是 UITabBarItem 的属性,而 UITabBarItem 是 UITabBarController 的属性,我知道我可以通过 super.TabBarController 访问 UITabBarController,但不知道如何更新某个 UITabBarItem。跨度>
      猜你喜欢
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2021-03-07
      • 2013-12-20
      相关资源
      最近更新 更多