简体   繁体   中英

Tab Bar not recognising ViewControllers when they are embedded in Navigation Controllers swift

I have an arrangement of views in my interface builder (image).

Red: My root TabBarController that segues to...
Yellow: My UINavControllers that have embedded...
Green: View Controllers

查看排列

I was trying to add code to TabBarController.swift to change how my ViewControllers.swift are presented (modal presentation code).

In TabBarController.swift

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController is CreationViewController {
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "CreationVC")
            
            controller.modalPresentationStyle = .fullScreen
            self.present(controller, animated: true, completion: nil)
            print("hello")
        } else { print("spaghet") }
    }
}

When running my application, the code is not recognised when I touch the CreationViewController tab bar item, even though the CreationViewController is being presented. Console prints "spaghet" not "hello".

So I changed the line

if viewController is CreationViewController

to

if tabBarController.selectedIndex == 1

and it now works. I don't understand why the view is not being recognised when I am using "if ViewController is CreationViewController", and why it is only being recognised when I use "TabBarController.selectedIndex". It is as if the Tab Bar Controller does not recognise what View it is on. By the way, I have provided the correct class (CreationViewController) and Storyboard ID to the ViewController in Interface Builder (the green circled one)

That's because the presented view controller by UITabBarController is actually UINavigationController. So it is not matching the class when you check it with

if viewController is CreationViewController

You can test it by simply printing the view controller you get from 'func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)' or putting a break point to that function.

The tab bar controller is holding a UINavigationController , which may or may not be holding your CreationViewController .

You want to test for it like this:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

    // make sure the selected tab contains a UINavigationController
    guard let navVC = viewController as? UINavigationController else {
        print("Selected tab does not contain a navigation controller")
        return
    }
    if navVC.visibleViewController is CreationViewController {
        print("CreationViewController is showing in selected tab's navigation controller")
        // do something
    } else {
        print("Some other controller is showing in selected tab's navigation controller")
        // do something else
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM