ios - 登录视图控制器不想在登录后关闭
问题描述
我想问我有一个登录视图控制器,在正确添加电子邮件和密码后不想关闭。但是当我第一次尝试模拟器时,登录工作正常并指向我的家庭控制器,但在我退出之后。并尝试再次登录,然后登录不会关闭我的登录视图控制器。这怎么可能?起初是工作,后来不工作,在这里我向您展示我的代码。
// this is my sign out button
@objc private func handleSignOut() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Log Out".localized(), style: .destructive, handler: { (_) in
self.progressHUD.show(in: self.view)
ProfileServices.shared.signOutUser { success in
if success {
self.progressHUD.dismiss(animated: true)
let signInVC = SigninViewController()
self.present(signInVC, animated: true, completion: nil)
} else {
self.progressHUD.textLabel.text = "Error"
self.progressHUD.dismiss(afterDelay: 0.4)
}
}
}))
alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
// this is my sign out function in ProfileServices.shared
func signOutUser(completion: @escaping (Bool) -> Void) {
AF.request(API_URL.AUTHENTICATION.LOGOUT, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: HEADERS, interceptor: nil).responseData { (dataResponse) in
if dataResponse.error == nil {
let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.synchronize()
UserDefaults.removeToken()
completion(true)
} else {
completion(false)
}
}
}
// this is my sign in route in my sign in view controller
func routeToMainView(_ data: SigninModel.Response) {
let school = UserDefaults.getSelectedSchool()
guard let schools = data.schools?.schools else { return }
if let selectedSchool = school, let selected = schools.first(where: { $0.id == selectedSchool.id}) {
UserDefaults.saveSelectedSchool(data: selected)
let vc = MainViewController()
self.viewController?.navigationController?.setViewControllers([vc], animated: true)
} else {
if schools.count > 1 {
let vc = SwitchSchoolViewController()
self.viewController?.navigationController?.setViewControllers([vc], animated: true)
} else {
guard let selected = schools.first else { return }
UserDefaults.saveSelectedSchool(data: selected)
DispatchQueue.main.async {
let vc = MainViewController()
self.viewController?.navigationController?.setViewControllers([vc], animated: true)
}
}
}
}
// this is in my appDelegate
var root: UIViewController?
root = SigninViewController()
if UserDefaults.getToken() != nil {
root = MainViewController()
}
解决方案
在注销时,您需要关闭呈现的 viewController。
到位 :
let signInVC = SigninViewController()
self.present(signInVC, animated: true, completion: nil)
你需要使用:
self.dismiss(animated: true, completion: nil)
或者如果您将使用 Push,则弹出。
推荐阅读
- mongodb - 如何通过SpringBoot rest api中的POST方法检查不同集合中的特定项目是否存在
- javascript - 如何将嵌套数据传递给 FlatList react-native
- typescript - 在打字稿中,是否可以为 Function.prototype.bind polyfill 正确定义类型?
- azure - Azure 虚拟机策略未在太平洋时间启动
- python - 即使在 Django 中选择选项后也没有?
- docker - 使用 DevOps 管道创建应用程序的分发包
- javascript - ReactJS + Typescript 中的条件 setInterval 和 clearInterval
- python-2.7 - 我根据 Pyhton 中的距离将 MoveTo 和 VectorReverse 命令应用于点。它不会失败,但不起作用。我能做些什么?
- java - 重定向 System.in ByteArrayInputStream 读取一个 JUnit 测试太晚了
- c# - Oracle.EntityFrameworkCore 发行说明