Why DetailViewController pop to first DetailViewCo

2019-08-16 15:28发布

问题:

First of all I am using storyboard. My ViewController hierarchy like this NavigationController -> SplashScreen -> LoginScreen -> MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController.

When I click a button on the DetailViewController page does not back to MainViewController. It is going to LoginScreen.

I tried this codes within addToBasket action in DetailViewController.

@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)

    }

Here is my loginButton codes for make MyTabBarController as rootViewController.

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

回答1:

In your approach the RootViewController is Navigation controller of Login screen therefore this issue occurs.

Your approach should be like as follows:

A) NavigationController -> SplashScreen -> LoginScreen

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

When user logged in, you should replace your window.rootViewController with B) MainTabBarController



回答2:

@IBAction func addBasket(_ sender: Any) {

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

    }