简体   繁体   English

如何展开导航堆栈上的第一个视图控制器

[英]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 {}

  • Add these above lines to your loginViewController.m file (or) add these lines to which view controller you want to move. 将以上行添加到loginViewController.m文件中(或)将这些行添加到要移动的视图控制器。
  • Actually you want to move to login view controller when clicking a button. 实际上,当您单击按钮时,您想要移动到登录视图控制器。 So,create a button or choose any button for this event. 因此,创建一个按钮或选择此事件的任何按钮。
  • Before doing this you want to add the above code to your login view controller. 在执行此操作之前,您需要将以上代码添加到登录视图控制器。 Now add action to your button by dragging into Exit option on view controller on the Top . 现在,通过拖动顶部视图控制器上的 Exit选项,为您的按钮添加操作。
  • It shows an unwind option unwindToCurrentController.Click that option and connect it. 它显示unwindToCurrentController的展开选项。单击该选项并连接它。

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.

相关问题 以编程方式推送至导航堆栈中的视图控制器[2],然后展开至[1] - Programmatically push to view controller [2] in a navigation stack, and unwind to [1] 首先启动导航堆栈的第二个视图控制器 - Start second view controller of navigation stack first 展开到不在当前导航视图控制器链中的viewController - Unwind to a viewController that is not in the current navigation view controller chain 如何在导航堆栈中识别以前的视图 controller - How to identify previous view controller in navigation stack 从导航控制器到视图控制器的Swift自定义展开Segue - Swift custom unwind Segue from Navigation controller to View Controller 如何从导航控制器中的viewcontrollers堆栈中获取特定的视图控制器? - How to get a particular view controller from the stack of viewcontrollers in the navigation controller? 如何在不删除整个堆栈的情况下关闭导航控制器中的视图控制器 - How to dismiss a view controller in a navigation controller, without dismissing the whole stack 弹出以查看导航堆栈中的控制器 - Pop to view controller in navigation stack 如何在导航视图控制器中关闭整个视图控制器堆栈? - How to dismiss a whole stack of view controllers in navigation view controller? 如何将segue解散到调用它的视图控制器 - how to unwind segue to the view controller that called it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM