【问题标题】:Updating TabBar Badge in Swift Coulld not cast value of type UINavigationController在 Swift 中更新 TabBar 徽章无法转换 UINavigationController 类型的值
【发布时间】:2015-09-14 15:32:52
【问题描述】:

我尝试为在情节提要上创建的标签栏中的标签栏项目设置一个值,每次通过我的应用程序委托推送远程通知时。我不断收到此错误:

 Could not cast value of type 'UINavigationController' (0x19632c0b0) to 'UITabBarController' (0x19632d230).

代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    var tabBarController = self.window!.rootViewController as! UITabBarController
        ...
        var tabArray = tabBarController.tabBar.items as NSArray!
        var tabItem = tabArray.objectAtIndex(3) as! UITabBarItem
        tabItem.badgeValue = "34"
        ...
    }
    println("notification")
    //scheduleNotification()
}

【问题讨论】:

  • 可以看到你的故事板的布局吗?

标签: swift uinavigationcontroller push-notification uitabbarcontroller


【解决方案1】:

您发布的代码的第一行

var tabBarController = self.window!.rootViewController as! UITabBarController

试图强制向下转换为UITabBarController,但 self.window!.rootViewController 是UINavigationController。在故事板中,确保标签栏控制器设置为“初始视图控制器”。

【讨论】:

  • 但是当我将标签栏控制器设置为初始视图控制器时,一切都搞砸了。
  • @AhmedNassar 可以看到你的故事板的截图吗?
猜你喜欢
  • 2018-11-02
  • 1970-01-01
  • 2014-08-25
  • 2018-04-22
  • 2016-12-21
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多