2

My goal is to display a tab view controller that manages multiple tabs that consist of navigation controllers containing view controllers.

I set the tab view controller BaseTabBarController as window my root view controller in AppDelegate. My custom tab view controller looks like this:

class BaseTabBarController: ESTabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        let v1 = BaseNavigationController(rootViewController: SubscriptionsController())
        let v2 = BaseNavigationController(rootViewController: SubscriptionsController())

        v1.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))
        v2.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))

        self.viewControllers = [v1, v2]
        self.hidesBottomBarWhenPushed = true
    }
}

My custom navigation controller class is an empty subclass of a navigation controller.

The problem is that the app displays a tab bar for a fraction of a second, and immediately turns into a black screen (console message: "Presenting view controllers on detached view controllers is discouraged"). What did I do wrong?

Cesare
  • 9,139
  • 16
  • 78
  • 130

1 Answers1

1

there has got to be something wrong with some other parts of your code. when i take your code and use it like this everything works as expected:

class BaseTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        let v1 = UINavigationController(rootViewController: UIViewController())
        let v2 = UINavigationController(rootViewController: UIViewController())

        v1.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)
        v2.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)

        self.viewControllers = [v1, v2]
        self.hidesBottomBarWhenPushed = true
    }
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = BaseTabBarController()
    window?.makeKeyAndVisible()

    return true
}
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • Thanks. You're 100% right. The issue is the root view controller of the navigation controllers. It looks like this: `class SubscriptionsController: UIViewController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { ... }`. If I change this back to a standard `UIViewController()` and no longer `SubscriptionsController()`, it works just fine. Any idea why? – Cesare Mar 03 '17 at 21:56
  • If you show me some more of the SubscriptionsController's implementation, maybe I can get an idea – André Slotta Mar 03 '17 at 21:57
  • For sure, thanks! https://gist.github.com/CeceXX/895f7812b1727dc05523eb763cc56b56 It's a standard view controller which loads a table view – Cesare Mar 03 '17 at 22:00
  • well... since i do not know how exactly your app works i would try and comment out all the `(self).present[...]` calls and try again. then i would comment them in one by one and see where it is going wrong. – André Slotta Mar 03 '17 at 22:05
  • oh wow, great tip. THANKS SO MUCH. I was presenting a totally random view controller. – Cesare Mar 03 '17 at 22:10
  • Glad I could help – André Slotta Mar 03 '17 at 22:11