material components tab bar controller error

1.5k views Asked by At

Receiving error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid item'" when selecting a tab bar item using MDCTabBarViewController. View controller loads but crashes right after selecting the second tab.

    class BusTabBarController: MDCTabBarViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            view.backgroundColor = .white
            loadTabBar()
        }

        func loadTabBar() {

            let firstVC = BusStopFlexibleHeaderContainerViewController()
            let secondVC = BookmarksFlexibleHeaderContainerViewController()
            let thirdVC = UIViewController()

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

            let childVC = viewControllers.first
            selectedViewController = childVC

            tabBar?.delegate = self

            tabBar?.items = [UITabBarItem(title: "Recents", image: nil, tag: 0),
                         UITabBarItem(title: "Favorites", image: nil, tag: 0),                                                           
                         UITabBarItem(title: "YAY", image: nil, tag: 2)]

            tabBar?.selectedItem = tabBar?.items.first

            tabBar?.backgroundColor = MDCPalette.grey.tint900
            tabBar?.selectedItemTintColor = .white
            tabBar?.unselectedItemTintColor = MDCPalette.grey.tint400
            tabBar?.inkColor = MDCPalette.blueGrey.tint100

        }

Any help is appreciated. Thanks!

1

There are 1 answers

0
gustavo.a.hansen On BEST ANSWER

I have the same problem. In the implementation of the MDCTabBarViewController when the

-(void)setSelectedViewController:(nullable UIViewController *)selectedViewController 

is fired there is a line where the code does:

self.tabBar.selectedItem = selectedViewController.tabBarItem;

So if you don't set the barItem to the controller you will get that error cause the reference is nil

I updated your code, so I hope this resolve your issue (I install the pod version 26)

class BusTabBarController: MDCTabBarViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .white
    loadTabBar()
}

func loadTabBar() {

    let firstVC = BusStopFlexibleHeaderContainerViewController()
    firstVC.tabBarItem = UITabBarItem(title: "Recents", image: nil, tag: 0)

    let secondVC = BookmarksFlexibleHeaderContainerViewController()
    secondVC.tabBarItem =  UITabBarItem(title: "Favorites", image: nil, tag: 0)

    let thirdVC = UIViewController()
    thirdVC.tabBarItem = UITabBarItem(title: "YAY", image: nil, 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.grey.tint900
    tabBar?.selectedItemTintColor = .white
    tabBar?.unselectedItemTintColor = MDCPalette.grey.tint400
    tabBar?.inkColor = MDCPalette.blueGrey.tint100

}
}