rootViewControllers при включении splitViewControllers - PullRequest
0 голосов
/ 23 апреля 2020

Следующий код работал, когда у меня было только wordsView в качестве имени класса для одного из моих rootviewControllers. Когда я перешел к splitviewcontroller, который использует классы wordsView и wordsDetailView, оператор переключения rootViewController никогда не вызывает ни один из классов, если они указаны как регистр.

Я подозреваю, что мне нужно изменить определение rootViewController в моем операторе guard, чтобы разместить контроллер splitview?

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
    }
...