【问题标题】:How to dismiss pushed ViewController after logout?注销后如何关闭推送的 ViewController?
【发布时间】:2020-09-20 03:48:56
【问题描述】:

我在这个问题上花了很多时间,所以我找不到任何解决方案。

我使用pushViewController 方法(这里是问题)和菜单(非模态)从我的TopBar 点击头像打开SettingsViewController。

当我点击注销按钮时,我想关闭这个推送的 ViewController。

下面是我在一些 VC 中使用的一个功能,效果很好。

func goToSettingsView() {
    let vc = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
    vc.modalPresentationStyle = .fullScreen
    self.navigationController!.pushViewController(vc, animated: true)
    for constraint in vc.view.constraints {
        if constraint.identifier == "alignTopHeader" {
           constraint.constant = 0
        }
    }
}

当我单击注销按钮不起作用并且当我从 (LoginViewController) 登录时,此 SettingsViewController 仍然显示,但我会在没有任何模式的情况下进入主屏幕。

我做了一些想法,但还没有很好地工作。

第一个想法是:

下面是我在 SettingsViewController 中的注销 IBaction:

- (void)logout {
    [self dismissViewControllerAnimated:YES completion:nil];
    [[UserManager shared] logoutUserWithSuccessBlock:^{
        [self presentLoginViewController];
    }];
}

LoginViewController 已关闭,但 self 是 SettingsViewController 的目标?

第二个想法:

我在 LoginViewController 的 AppDelegate "backToRoot" 中添加了一个声明的函数,并从 viewWillDisappear 调用。

[appDelegate backToRoot];

AppDelegate.m 文件中的函数

-(void)backToRoot {
   [self.navigationController dismissViewControllerAnimated:YES completion:nil];
   PresentationsPickerViewController *mainvc = [[PresentationsPickerViewController alloc] init];
   [self setCenterPanelWithViewController:mainvc];
}

但仍然不能使用模态,当 SettingsViewController 不是模态时它可以正常工作。

您知道如何在注销操作中隐藏/关闭推送的 SettingsViewController 吗?

【问题讨论】:

    标签: ios objective-c swift uiviewcontroller


    【解决方案1】:

    这真的很简单。如果您想在导航堆栈中关闭推送的UIViewController,只需将其弹出,如下所示:

    func logout() {
        navigationController?.popViewController(animated: true)
    }
    

    【讨论】:

      【解决方案2】:

      您可以简单地关闭根视图控制器上方的所有视图控制器。

      func logout() {
        self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
      }
      

      希望它会有所帮助。谢谢。

      【讨论】:

        【解决方案3】:

        当我点击注销按钮时,我想关闭这个推送的 ViewController。

        现在您不会关闭推送的UIViewController。 Push 是与UINavigationController 关联的导航样式。当您推送 UIViewController 时,您需要弹出它。

        当我们谈论解雇时,我们通常是指关闭以模态呈现的UIViewController。此方法意味着在您之前显示的UIViewController 或任何继承自它的类之上添加一个新的UIViewController。这不会添加到导航堆栈中。您只能在 UIViewController 出现时将其关闭。

        现在您的第一个代码块显示您推送SettingsViewController,而您的第一个解决方案尝试将其关闭。这行不通。您需要从UINavigationController 弹出它才能关闭它。

        接下来,

        LoginViewController 已关闭,但 self 是 SettingsViewController 的目标?

        [self dismiss]... 方法将关闭最上面显示的屏幕。如果您在设置中显示LoginViewController,则LoginViewController 屏幕将被关闭。

        还有,

        但仍然不能使用模态,当 SettingsViewController 不是模态时它可以正常工作。您对如何在注销操作中隐藏/关闭推送的 SettingsViewController 有任何想法吗?

        如果你的SettingsViewController 出现了,那么你需要关闭它,如果它被推送,你需要弹出它。

        如果存在两种操作都可能发生的情况,则在关闭按钮操作中,您可以检查屏幕的显示方式。使用this 链接找出检查。

        如果您只想在注销时转到您的LoginViewController,您只需更改应用程序的根窗口即可。

        let window = (UIApplication.shared.delegate as? AppDelegate)?.window
        window?.rootViewController = viewController //this will be your loginViewController
        window?.makeKeyAndVisible()
        

        对于 iOS 13:

        UIApplication.shared.windows.first?.rootViewController = LoginViewController
        UIApplication.shared.windows.first?.makeKeyAndVisible()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-19
          • 1970-01-01
          • 2022-11-20
          相关资源
          最近更新 更多