如何退回到导航堆栈上的第一个视图控制器 [英] How to unwind to the first view controller on a navigation stack
问题描述
我在将视图展开到登录屏幕时遇到问题。故事板的结构如下:
故事板结构
I have a problem with unwinding my view to login screen. The storyboard structure is as follow: Storyboard Structure
应用程序的用户流程如下:$ VC $上的
用户登录,在每个选项卡栏项上通过modal segue->进入主选项卡屏,导航控制器上的bar按钮可访问配置文件页面,每个选项卡栏项均具有独立的导航控制器,以保持导航控制器结构的线性。 ->单击个人资料页面按钮后,将以模式方式显示个人资料页面->单击个人资料页面上的注销按钮时,它会触发展开搜索并关闭视图控制器
The user flow for the app is as follow: user login on LoginVC-> goes to main tab bar screen by modal segue-> on each tab bar item, I added right bar button on the navigation controller to access profile page, each tab bar item has independent navigation controller to keep the nav controller structure linear. -> once i click profile page button, profile page is presented modally -> when logout button on profile page is clicked, it triggers unwind segue and dismiss view controller
func logoutUser(){
//Networking.logoutUser()
print("It goes to login")
self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}
展开登录是在LoginVC上实现的最左侧的VC.I在配置文件屏幕上连接了unwind segue,并将其称为 unwindToLogin。
我只使用了performSegueWithIdentifier。但是,该方法不会被调用,并且视图不会发生任何事情。
The unwind segue was implemented on LoginVC on the leftmost VC.I connected unwind segue on the profile screen and call it "unwindToLogin" I simply used performSegueWithIdentifier. However, the method does not get called and nothing happen to the view.
编辑1:
我想知道自从我在选项卡栏vc上以模式方式调用个人资料页面后,找不到unwindtologin。
如果我只是使用InstantiateViewContoller调用登录,是否会清除我的视图控制器堆栈?
Edit 1: Im wondering since i call profile page modally on tab bar vc, it couldnt find the unwindtologin. If i simply use the instantiateviewcontoller to call login, will it clear my view controller stack ?
编辑2:
对不起,我忘了当我检查用户是否登录时,我使用下面的代码:
Edit 2: Sorry, i forgot that when i check if a user is logged in, i use the code below:
if (FIRAuth.auth()?.currentUser != nil) {
self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
}
所以实际上他们找不到展开方法,因为根视图不是主登录视图控制器。
So actually they can't find the unwind method because the root view is not main login view controller.
有人可以帮忙吗?
推荐答案
您需要在要调用的每个ViewController上创建展开序列。他们都可以在登录屏幕中使用相同的 @IBAction
目标,都可以称为 unwindToLogin
。
You need to create the unwind segue on every ViewController that you want to call it on. They can all use the same @IBAction
destination in the login screen and they can all be called "unwindToLogin"
.
我看到在 MainTabBarController
上定义的展开顺序。在您的 UserProfileController
中,您需要 control -从 view Controller 图标拖动到 Exit 图标,然后在弹出窗口中选择您的 @IBAction
。然后在 Document Outline 的 UserProfileController
中找到展开选项,并使其标识符为 unwindToLogin
。
I see the unwind segue defined on the MainTabBarController
. In your UserProfileController
, you need to control-drag from the view Controller icon to the Exit icon and then select your @IBAction
in the pop up. Then find that unwind segue in the UserProfileController
in the Document Outline and make its identifier "unwindToLogin"
.
回应您的 Edit 2:
由于您最初的viewController是通过编程方式放置的,因此无法展开到 LoginViewController
。在这种情况下,
将着陆 @IBAction
放入您的 MainTabBarController
中,并将其设置为 self.window?.rootViewController
进入登录屏幕。
Since your initial viewController was put into place programmatically, it isn't possible to unwind to the LoginViewController
. In this case,
put the landing @IBAction
in your MainTabBarController
and have it set the self.window?.rootViewController
to the login screen.
这篇关于如何退回到导航堆栈上的第一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!