使用MDCTabBarViewController时空白的ViewController-Swift 4.2

时间:2018-09-19 22:10:37

标签: ios swift uiviewcontroller material-components

我遇到了MDCTabBarViewController的问题,因为所有ViewController都显示为空白(例如:使用情节提要或通过view.addSubview()添加视图),但似乎唯一受影响的似乎是ViewController的背景 (例如:view.backgroundColor = UIColor.green

TabViewController:

import UIKit
import MaterialComponents

class TabViewController: MDCTabBarViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //view.backgroundColor = MDCPalette.grey.tint100
    loadTabBar()
}

func loadTabBar() {

    let firstVC = FeedViewController()
    firstVC.tabBarItem = UITabBarItem(title: "Feed", image: #imageLiteral(resourceName: "feed"), tag: 0)

    let secondVC = TableViewController()
    secondVC.tabBarItem =  UITabBarItem(title: "Timetable", image: #imageLiteral(resourceName: "table"), tag: 1)

    let thirdVC = ToDoViewController()
    thirdVC.tabBarItem = UITabBarItem(title: "To-Do", image: #imageLiteral(resourceName: "todo"), tag: 2)

    let viewControllersArray = [firstVC, secondVC, thirdVC]
    viewControllers = viewControllersArray

    let childVC = viewControllers.first
    selectedViewController = childVC

    tabBar?.delegate = self

    tabBar?.items = [firstVC.tabBarItem,
                     secondVC.tabBarItem ,
                     thirdVC.tabBarItem]

    tabBar?.selectedItem = tabBar?.items.first

    tabBar?.backgroundColor = MDCPalette.lightBlue.tint500
    tabBar?.selectedItemTintColor = .white
    tabBar?.unselectedItemTintColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3)
    tabBar?.inkColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1)
    tabBar?.alignment = .justified


}

override func tabBar(_ tabBar: MDCTabBar, didSelect item: UITabBarItem) {

    switch item.tag {
    case 0:
        print("feed")
        tabBar.backgroundColor = MDCPalette.lightBlue.tint500
        selectedViewController = viewControllers[0]
    case 1:
        print("table")
        tabBar.backgroundColor = MDCPalette.purple.tint500
        selectedViewController = viewControllers[1]
    case 2:
        print("todo")
        tabBar.backgroundColor = MDCPalette.teal.tint500
        selectedViewController = viewControllers[2]
    default:
        print("feed")
        tabBar.backgroundColor = MDCPalette.lightBlue.tint500
        selectedViewController = viewControllers[0]
    }

}


}

所有FeedViewController,TableViewController和ToDoController是新创建的文件,其中没有代码。

view.backgroundColor = UIColor.greenenter image description here

添加标签view.addSubview(label)enter image description here

预先感谢一堆!

更新:

通过情节提要添加视图并尝试访问该视图时,应用程序崩溃,提示标签为空

FeedViewController:

import UIKit

class FeedViewController: UIViewController {

@IBOutlet weak var test: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.green
    print(test.text ?? "unable to fetch")
}

} 

错误:致命错误:在展开可选值时意外发现nil

1 个答案:

答案 0 :(得分:1)

尝试一下:

override func viewDidLoad() {
    super.viewDidLoad()

    view?.backgroundColor = UIColor.green
    print(test?.text ?? "unable to fetch")
}