Search code examples
iosswiftuinavigationcontrolleruitabbarcontrollerviewcontroller

Back Button Doesn't Work When I switched to another ViewController inside of TabBarController


Back Button Doesn't Work When I switch to another ViewController inside of TabBarController and trying to go back.

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let detailVC = NewDetailController()
    navigationController?.pushViewController(detailVC, animated: true)
    }

I think the problem is in the way how I am creating detailVC. But not sure what's wrong.

Creating TabBarController:

class MainTabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    // New View Controller

    let newController = NewController()
    let newNavController = UINavigationController(rootViewController: newController)

    viewControllers = [newNavController]
} }

Set UITabBarController as initial in AppDelegate:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let mainVC = MainTabBarController()
    window?.rootViewController = UINavigationController(rootViewController: mainVC)

    return true
}

BackButton which doesn't work

Before I set UITabBarController as my rootVC everything worked fine.

PS: I am not using a storyboard. Everything is done programatically.


Solution

  • Ohh Well Thanks to Muhammad he gave me a crucial point.

    The problem is that I am embedding my TabBarController into UINavigationController in AppDelegate.

    When I remove UINavigationController everything works great!

        let mainVC = MainTabBarController()
        window?.rootViewController = mainVC