问题
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