![](/img/trans.png)
[英]Programmatically push to view controller [2] in a navigation stack, and unwind to [1]
[英]How to unwind to the first view controller on a navigation stack
我有一个问题,将我的视图展开到登录屏幕。 故事板结构如下: 故事板结构
该应用程序的用户流程如下:用户登录LoginVC->通过模式segue->在每个标签栏项目上转到主标签栏屏幕,我在导航控制器上添加右栏按钮以访问配置文件页面,每个标签栏item具有独立的导航控制器,使导航控制器结构保持线性。 - >一旦我点击个人资料页面按钮,个人资料页面以模态方式显示 - >点击个人资料页面上的注销按钮时,它会触发展开segue并关闭视图控制器
func logoutUser(){
//Networking.logoutUser()
print("It goes to login")
self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}
展开segue是在最左边的VC.I连接展开segue上的LoginVC上实现的,并将其称为“unwindToLogin”我只是使用performSegueWithIdentifier。 但是,该方法不会被调用,视图也不会发生任何事情。
编辑1:我想知道,因为我在标签栏vc上模拟调用配置文件页面,它无法找到unwindtologin。 如果我只是使用instantiateviewcontoller来调用login,它会清除我的视图控制器堆栈吗?
编辑2:抱歉,我忘了当我检查用户是否登录时,我使用下面的代码:
if (FIRAuth.auth()?.currentUser != nil) {
self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
}
实际上他们找不到unwind方法,因为根视图不是主登录视图控制器。
有人可以帮忙吗?
使用展开segue移动到任何View控制器。
单击按钮时移动到任何视图控制器。
- (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue {}
现在构建并运行您的应用程序。 它完美地运作。
您需要在要调用它的每个ViewController上创建展开segue。 它们都可以在登录屏幕中使用相同的@IBAction
目的地,它们都可以称为"unwindToLogin"
。
我在MainTabBarController
上看到了unwind segue。 在UserProfileController
,您需要从视图Controller图标控制 -drag到Exit图标,然后在弹出窗口中选择您的@IBAction
。 然后在Document Outline中的UserProfileController
中找到unwind segue,并使其标识符为"unwindToLogin"
。
回应你的编辑2:
由于您的初始viewController是以编程方式放置的,因此无法展开到LoginViewController
。 在这种情况下,将着陆@IBAction
放入MainTabBarController
并让它将self.window?.rootViewController
设置为登录屏幕。
你可以试试这个为我工作。
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
我用它来消除模态视图,以便我可以返回到原始视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.