如何以正确的方式返回初始 ViewController? [英] How to go back to the initial ViewController the right way?
问题描述
我有几个带有导航栏和标签栏的视图控制器.然后我有一个没有导航栏和标签栏的初始视图控制器.我想回到初始视图控制器,堆栈中没有其他视图控制器.这就是我所做的:
I have a few view controllers with navigation bar and tab bar. Then I have an initial view controller with no navigation bar and tab bar. I want to go back to the initial view controller with no other view controllers in stack. Here's what I do:
// Head back to Initial View Controller.
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "Initial")
UIApplication.shared.keyWindow?.rootViewController = initialViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = initialViewController
这完美地工作.但是它不会再次从初始视图控制器移动到另一个视图控制器.例如:
This works perfectly. But then it doesn't move to another view controller from the initial view controller again. For example:
@IBAction func loginButton(_ sender: Any) {
print("Login Button Tapped.")
let storyboard = UIStoryboard(name: "SignInViewController", bundle: nil)
let signInViewController = storyboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
self.navigationController?.show(signInViewController, sender: nil)
}
在这里,print 语句会运行,但 self.navigationController.show
不会.这一切如何运作?实现这一目标的正确方法是什么?
Here, the print statement is run, but self.navigationController.show
won't. How do all this work? And what's the right approach to accomplish this?
为避免混淆,这是我的故事板.最左边的视图控制器(粉红色屏幕)是我想要返回的初始视图控制器.同层最右边是我想回去的景色.
To avoid any confusion, here's my storyboard. The left most viewcontroller (pink screen) is the initial view controller I want to go back to. The right most on the same level is the view where I want to go back from.
推荐答案
它不会移动到另一个视图控制器,因为您正在 rootViewController 中设置一个简单的视图控制器.
It doesn't move to another view controller, because you are setting a simple view controller in your rootViewController.
你需要的是:
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "Initial")
let navController = UINavigationController.init(rootViewController: initialViewController)
UIApplication.shared.keyWindow?.rootViewController?.present(navController, animated: true, completion: nil)
这将创建一个导航视图控制器,因此在初始"时,您应该能够执行推送,呈现另一个视图控制器的操作.
This will create a navigationviewcontroller, so when on "Initial" you should be able to perform push, present actions of another view controllers.
这篇关于如何以正确的方式返回初始 ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!