[英]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
故事板结构如下: 故事板结构
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. 该应用程序的用户流程如下:用户登录LoginVC->通过模式segue->在每个标签栏项目上转到主标签栏屏幕,我在导航控制器上添加右栏按钮以访问配置文件页面,每个标签栏item具有独立的导航控制器,使导航控制器结构保持线性。 -> 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
- >一旦我点击个人资料页面按钮,个人资料页面以模态方式显示 - >点击个人资料页面上的注销按钮时,它会触发展开segue并关闭视图控制器
func logoutUser(){
//Networking.logoutUser()
print("It goes to login")
self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}
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. 展开segue是在最左边的VC.I连接展开segue上的LoginVC上实现的,并将其称为“unwindToLogin”我只是使用performSegueWithIdentifier。 However, the method does not get called and nothing happen to the view.
但是,该方法不会被调用,视图也不会发生任何事情。
Edit 1: Im wondering since i call profile page modally on tab bar vc, it couldnt find the unwindtologin. 编辑1:我想知道,因为我在标签栏vc上模拟调用配置文件页面,它无法找到unwindtologin。 If i simply use the instantiateviewcontoller to call login, will it clear my view controller stack ?
如果我只是使用instantiateviewcontoller来调用login,它会清除我的视图控制器堆栈吗?
Edit 2: Sorry, i forgot that when i check if a user is logged in, i use the code below: 编辑2:抱歉,我忘了当我检查用户是否登录时,我使用下面的代码:
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. 实际上他们找不到unwind方法,因为根视图不是主登录视图控制器。
Can anyone help ? 有人可以帮忙吗?
Move to any View controller using unwind segue. 使用展开segue移动到任何View控制器。
For moves to any view controller when you clicking a button. 单击按钮时移动到任何视图控制器。
- (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue { } - (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue {}
Now Build and run your app. 现在构建并运行您的应用程序。 It works perfectly.
它完美地运作。
You need to create the unwind segue on every ViewController that you want to call it on. 您需要在要调用它的每个ViewController上创建展开segue。 They can all use the same
@IBAction
destination in the login screen and they can all be called "unwindToLogin"
. 它们都可以在登录屏幕中使用相同的
@IBAction
目的地,它们都可以称为"unwindToLogin"
。
I see the unwind segue defined on the MainTabBarController
. 我在
MainTabBarController
上看到了unwind segue。 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. 在
UserProfileController
,您需要从视图Controller图标控制 -drag到Exit图标,然后在弹出窗口中选择您的@IBAction
。 Then find that unwind segue in the UserProfileController
in the Document Outline and make its identifier "unwindToLogin"
. 然后在Document Outline中的
UserProfileController
中找到unwind segue,并使其标识符为"unwindToLogin"
。
In response to your Edit 2: 回应你的编辑2:
Since your initial viewController was put into place programmatically, it isn't possible to unwind to the LoginViewController
. 由于您的初始viewController是以编程方式放置的,因此无法展开到
LoginViewController
。 In this case, put the landing @IBAction
in your MainTabBarController
and have it set the self.window?.rootViewController
to the login screen. 在这种情况下,将着陆
@IBAction
放入MainTabBarController
并让它将self.window?.rootViewController
设置为登录屏幕。
You could try this it Worked for me. 你可以试试这个为我工作。
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
I use it to dismiss a modal view so that I can return to the originating view. 我用它来消除模态视图,以便我可以返回到原始视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.