为什么 DetailViewController 弹出到第一个带有 Tabbar 和 Navigation Controller 的 DetailViewController?

Why DetailViewController pop to first DetailViewController with Tabbar and Navigation Controller?

首先我使用的是故事板。我的 ViewController 层次结构像这样 NavigationController -> SplashScreen -> LoginScreen -> MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController

当我单击 DetailViewController 页面上的按钮时,不会返回到 MainViewController。即将 LoginScreen

我在 DetailViewControlleraddToBasket 操作中尝试了此代码。

@IBAction func addBasket(_ sender: Any) {

SingletonCart.sharedFood.food.append(food!)

let mainView = self.storyboard?.instantiateViewController(withIdentifier: "FoodOrder") as! MainViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate

self.navigationController?.popViewController(animated: true)
        dismiss(animated: true)

    }

这是我的 loginButton 代码,用于将 MyTabBarController 作为 rootViewController。

 @IBAction func loginButton(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController")
    self.window?.rootViewController = viewController
}
@IBAction func addBasket(_ sender: Any) {

        SingletonCart.sharedFood.food.append(food!)
        self.navigationController?.popViewController(animated: true)

    }

在您的方法中,RootViewController 是登录屏幕的导航控制器,因此会出现此问题。

您的方法应该如下所示:

A) NavigationController -> SplashScreen -> LoginScreen

B) MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController

当用户登录时,您应该将 window.rootViewController 替换为 B) MainTabBarController