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

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.

评论
  • bsequi
    bsequi 回复
    如果我的流程是LoginViewController-> MainTabBarController->嵌入在MainTabBarController中的其他控制器,并且我想注销,如何在没有内存泄漏的情况下进入LoginViewController?

    yes popToRootViewController is the right option

    但这实际上并没有用,因为navigationController为零

    这意味着loginVc不在导航内