为什么即使我按下导航控制器也为零

I have a LoginViewController and want to push MainTabBarController So, I do this

let controller = MainTabBarController()
navigationController?.pushViewController(controller, animated: true)

LoginViewController has a navigation controller but MainTabBarController doesn't for some reasons.

I have two questions, if my flow is LoginViewController -> MainTabBarController -> Other Controllers embedded in MainTabBarController, and I want to log out, how do I get to the LoginViewController without memory leaks? I was thinking about something like popToRootViewController but not sure if it would work.

This is how I'm creating other controllers in MainTabBarController

private func createNewViewController(viewController: UIViewController, title: String, imageName: String) -> UIViewController {
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.tabBarItem = UITabBarItem(title: title, image: UIImage(systemName: imageName), selectedImage: nil)
        navigationController?.view.backgroundColor = .white
        viewController.navigationItem.title = title
        return viewController
    }

But it doesn't really work since navigationController is nil. I don't use storyboards at all though.