【问题标题】:Cant add text to listview in Tab bars Swift2无法在标签栏 Swift 2 中将文本添加到列表视图
【发布时间】:2015-11-22 09:13:24
【问题描述】:

我使用此代码将标题设置为标签栏项目。它适用于前 5 个。但是当第六个应该在“更多”下的列表视图中添加文本时,应用程序崩溃。如何在该列表上设置文本?

    let tabArray = self.tabBarController?.tabBar.items as NSArray!

    let tabItem1 = tabArray.objectAtIndex(0) as! UITabBarItem
    let tabItem2 = tabArray.objectAtIndex(1) as! UITabBarItem
    let tabItem3 = tabArray.objectAtIndex(2) as! UITabBarItem
    let tabItem4 = tabArray.objectAtIndex(3) as! UITabBarItem
    let tabItem5 = tabArray.objectAtIndex(4) as! UITabBarItem
    let tabItem6 = tabArray.objectAtIndex(5) as! UITabBarItem

    tabItem1.title = "Home"
    tabItem2.title = NewArray().12
    tabItem3.title = NewArray().13
    tabItem4.title = NewArray().14
    tabItem5.title = NewArray().15
    tabItem6.title = NewArray().16

【问题讨论】:

    标签: swift tabbar tabbarcontroller


    【解决方案1】:

    试试这个:

    let viewControllerArray = self.tabBarController?.viewControllers as [UIViewController]!
    let tabItem1 = viewControllerArray[0].tabBarItem
    let tabItem2 = viewControllerArray[1].tabBarItem
    let tabItem3 = viewControllerArray[2].tabBarItem
    let tabItem4 = viewControllerArray[3].tabBarItem
    let tabItem5 = viewControllerArray[4].tabBarItem
    let tabItem6 = viewControllerArray[5].tabBarItem
    
    tabItem1.title = "Home"
    tabItem2.title = NewArray().12
    tabItem3.title = NewArray().13
    tabItem4.title = NewArray().14
    tabItem5.title = NewArray().15
    tabItem6.title = NewArray().16
    

    【讨论】:

    • 谢谢!但是我在第一行得到了这个错误: Downcast from '[UIViewController]?'到 [UIViewController]' 仅解开选项;你的意思是用'!'?'
    • @user5216681 已针对 Swift2 更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多