问题描述
首先我是使用故事板.我的 ViewController 层次结构像这样 NavigationController ->启动画面 ->登录屏幕 ->MainTabBarController ->MainNavigationController ->主视图控制器 ->细节视图控制器
.
First of all I am using storyboard. My ViewController hierarchy like this NavigationController -> SplashScreen -> LoginScreen -> MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController
.
当我点击 DetailViewController
页面上的按钮时,不会返回到 MainViewController
.它将转到 LoginScreen
.
When I click a button on the DetailViewController
page does not back to MainViewController
. It is going to LoginScreen
.
我在 DetailViewController
中的 addToBasket
操作中尝试了此代码.
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)
}
这是我将 MyTabBarController
设为 rootViewController 的 loginButton
代码.
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
}
推荐答案
在您的方法中,RootViewController 是登录屏幕的导航控制器,因此会出现此问题.
In your approach the RootViewController is Navigation controller of Login screen therefore this issue occurs.
您的方法应如下所示:
A) NavigationController -> SplashScreen -> LoginScreen
A) NavigationController -> SplashScreen -> LoginScreen
B) MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController
B) MainTabBarController -> MainNavigationController -> MainViewController -> DetailViewController
当用户登录时,你应该用 B) MainTabBarController 替换你的 window.rootViewController
When user logged in, you should replace your window.rootViewController with B) MainTabBarController
这篇关于为什么 DetailViewController 使用 Tabbar 和 Navigation Controller 弹出到第一个 DetailViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!