NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

rootViewControllers when splitViewControllers are invloved

Forums > iOS

The following code worked when I had just wordsView as the class name for one of my rootviewControllers. When I moved to a splitviewcontroller which uses wordsView and wordsDetailView classes, the rootViewController switch statement never triggers either class when they are stated as a case.

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        guard
            let navigationController = viewController as? UINavigationController,
            let rootViewController = navigationController.viewControllers.first // (1) derives the root view controller of the navigation controller preparing to be selected.
            else { return true }

        let shouldSelect: Bool
        switch rootViewController {
        case is wordsView:
            languages = modelView.loadLanguages()
            print("languages in case is wordsView in tabBar are: \(languages)")
            if languages.isEmpty {
                shouldSelect = false // (2a) restrict selection of 'Words' when no languages exist
            } else {
                shouldSelect = true // (2b) allow selection of 'Words' because language(s) exist
            }
            viewControllerCase = "words"
            print("viewControllerCase in tabBarController in tabBar is: \(viewControllerCase)")
            print("shouldSelect in wordsView case in tabBarController in tabBar is: \(shouldSelect)")
        case is homeworkView:
            languages = modelView.loadLanguages()
            words = loadWords()

            print("languages in case is homeworkView in tabBar are: \(languages)")
            print("words in case is homeworkView in tabBar are: \(words)")
            if words.isEmpty {
                shouldSelect = false // (2a) restrict selection of 'Homework' when no words exist
            } else {
                shouldSelect = true // (2b) allow selection of 'Homework' because word(s) exist
            }
            viewControllerCase = "homework"
            print("viewControllerCase in tabBarController in tabBar is: \(viewControllerCase)")
            print("shouldSelect in homeworkView case in tabBarController in tabBar is: \(shouldSelect)")
        case is testView:
            languages = modelView.loadLanguages()
            print("languages in case is testView in tabBar are: \(languages)")
            words = loadWords()
            print("words in case is testViewController in tabBar are: \(words)")
            if words.isEmpty {
                shouldSelect = false // (2a) restrict selection of 'Test' when no words exist
            } else {
                shouldSelect = true // (2b) allow selection of 'Test' because word(s) exist
            }
            print("shouldSelect in testView case in tabBarController in tabBar is: \(shouldSelect)")
            viewControllerCase = "test"
            print("viewControllerCase in tabBarController in tabBar is: \(viewControllerCase)")
            print("shouldSelect in testView case in tabBarController in tabBar is: \(shouldSelect)")
        case is languagesView:
            shouldSelect = true // (3) always allow selection of 'Languages' (from any other VC)

        case is moreView:
            shouldSelect = true // (3) always allow selection of 'More' (from any other VC)
        default:
            shouldSelect = true // (*) fallback to allow selection of any VC not explicitly handled above
        }
        if shouldSelect == false {
            print("languages are \(languages) and languages.isEmpty is \(languages.isEmpty) in tabBarController shouldSelect in tabBar")
            if languages.isEmpty && viewControllerCase == "words" {
                let ac = UIAlertController(title: "You can enter words once you add a language!", message: nil, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default)
                ac.addAction(okAction)
                present(ac, animated: true)
            } else if languages.isEmpty && viewControllerCase == "homework" {
                let ac = UIAlertController(title: "You can do homework once you add a language!", message: nil, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default)
                ac.addAction(okAction)
                present(ac, animated: true)
            } else if languages.isEmpty && viewControllerCase == "test" {
                let ac = UIAlertController(title: "You can be tested once you add a language!", message: nil, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default)
                ac.addAction(okAction)
                present(ac, animated: true)
            } else if  words.isEmpty && viewControllerCase == "homework" {
                let ac = UIAlertController(title: "You can do homework once you add words!", message: nil, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default)
                ac.addAction(okAction)
                present(ac, animated: true)
            } else if words.isEmpty && viewControllerCase == "test" {
                let ac = UIAlertController(title: "You can be tested once you add words!", message: nil, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default)
                ac.addAction(okAction)
                present(ac, animated: true)
            }
        }
        myTabBarDelegate?.shouldPerformSegue()
        return shouldSelect
    }

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.