ios - 注销后如何关闭推送的 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 吗?
解决方案
当我点击注销按钮时,我想关闭这个推送的 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()
推荐阅读
- java - 如何使用 Java 在 Swing GUI 中单击按钮时打开一个新窗口
- vhdl - ModelSim 错误加载设计只是一个小小的 hello world 程序?
- json - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte while inserting json data to database
- ios - 使用 bundle 解码本地 json 文件的预览过程似乎已崩溃
- javascript - 带瓦片源的三盒
- unit-testing - 从服务器渲染 html 页面后的客户端测试
- javascript - 如何在nodeJS中反转具有科学计数法的数值?
- angular - 当两个指令检查彼此是否存在时避免循环依赖注入?
- sql - 没有光标的 TSQL 帮助
- java - 如何从 android studios 中的对象类数组列表创建行?