UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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
    }

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.