首页 > 解决方案 > 注销后如何关闭推送的 ViewController?

问题描述

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

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 吗?

标签: iosobjective-cswiftuiviewcontroller

解决方案


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

现在你不要解雇 push UIViewController。Push 是一种与UINavigationController. 当你按下一个UIViewController你需要弹出它。

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

现在,您的第一个代码块显示您推动了SettingsViewController并且您的第一个解决方案试图解除它。这行不通。您需要将其从 中弹出以将UINavigationController其关闭。

下一个,

LoginViewController 被解雇,但 self 是针对 SettingsViewController 的?

该方法[self dismiss]...将关闭顶部最新显示的屏幕。如果您LoginViewController从设置中呈现,则LoginViewController屏幕将被关闭。

还,

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

如果SettingsViewController出现了,则需要将其关闭,如果已推送,则需要将其弹出。

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

如果您只想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()

推荐阅读