我知道有一些类似于这个问题的问题,然而,很多问题都在Objective C中,我还没有找到一个真正适用的解决方案。
我遇到的主要问题是,当我在我的应用程序中注销一个帐户,然后登录到另一个帐户时,标签栏不会重置,并显示以前登录的用户数据。换句话说,我需要一种方法将应用程序“重置”回任何用户登录之前的状态。
我试图通过在App Delegate(setupTabBarController)中编写一个函数并在用户注销时调用它来实现这一点,但是还没有这样的运气。
这是我到目前为止所做的:
退出代码:
@objc func handleSignOutButtonTapped() {
let signOutAction = UIAlertAction(title: "Sign Out", style: .destructive) { (action) in
do {
try Auth.auth().signOut()
let welcomeControl = WelcomeController()
let welcomeNavCon = UINavigationController(rootViewController: welcomeControl)
self.present(welcomeNavCon, animated: true, completion: nil)
} catch let err {
print("Failed to sign out with error", err)
Service.showAlert(on: self, style: .alert, title: "Sign Out Error", message: err.localizedDescription)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
Service.showAlert(on: self, style: .actionSheet, title: nil, message: nil, actions: [signOutAction, cancelAction]) {
}
let delegate = AppDelegate()
delegate.setupTabBarController()
}
我的App代表的一部分:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
setupTabBarController()
return true
}
func setupTabBarController() {
window = UIWindow()
window?.makeKeyAndVisible()
let vc = MainTabBarController()
let controller = UINavigationController(rootViewController: vc)
window?.rootViewController = controller
}
登录代码:
@objc func handleNormalLogin() {
hud.textLabel.text = "Signing In..."
hud.show(in: view, animated: true)
//TODO
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
print("Error signing in: \(error)")
return
}
//sucessfully signed in
self.hud.dismiss(animated: true)
self.dismiss(animated: true, completion: nil)
}
}
任何帮助都非常感谢,我现在已经坚持了几个小时,我真的想了解我做错了什么。
干杯
答案 0 :(得分:3)
<强>问题强>
撰写let delegate = AppDelegate()
时。这意味着您创建了一个新的AppDelegate
。您可以使用其他AppDelegate
,而不是使用当前AppDelegate
。这就是为什么setupTabBarController
方法不会影响任何事情的原因。
在setupTabBarController
结束时拨打handleSignOutButtonTapped
并不是一个好主意。因为它会将当前rootViewController
替换为UINavigation
MainTabBarController
。
<强>答案强>
self.tabBarController?
代替self
来展示welcomeNavCon
。setupTabBarController
。handleSignOutButtonTapped
重新创建并设置新viewControllers
。<强>代码强>
MainTabBarController
@objc func handleSignOutButtonTapped() {
let signOutAction = UIAlertAction(title: "Sign Out", style: .destructive) { (action) in
do {
try Auth.auth().signOut()
let welcomeControl = WelcomeController()
let welcomeNavCon = UINavigationController(rootViewController: welcomeControl)
self.tabBarController?.present(welcomeNavCon, animated: true) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate;
appDelegate.resetTabBarController();
};
} catch let err {
print("Failed to sign out with error", err)
Service.showAlert(on: self, style: .alert, title: "Sign Out Error", message: err.localizedDescription)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
Service.showAlert(on: self, style: .actionSheet, title: nil, message: nil, actions: [signOutAction, cancelAction]) {
}
}