gpt4 book ai didi

swift - 在 Swift 中更新 TabBar Badge 无法转换 UINavigationController 类型的值

转载 作者:行者123 更新时间:2023-11-30 14:16:35 24 4
gpt4 key购买 nike

每当通过我的应用程序委托(delegate)推送远程通知时,我都会尝试为在 Storyboard 上创建的选项卡栏中的选项卡栏项目设置一个值。我不断收到此错误:

 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()
}

最佳答案

您发布的代码的第一行

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

正试图强制向下转换为 UITabBarController,但 self.window!.rootViewController 是一个 UINavigationController。在 Storyboard中,确保将选项卡栏 Controller 设置为“初始 View Controller ”。

关于swift - 在 Swift 中更新 TabBar Badge 无法转换 UINavigationController 类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com