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

佐手、 提交于 2019-12-13 03:58:01

问题


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)

    }


来源:https://stackoverflow.com/questions/55951530/why-detailviewcontroller-pop-to-first-detailviewcontroller-with-tabbar-and-navig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!