ios - 注销并重置 UITabBarController

标签 ios swift firebase

我知道那里有一些与此问题类似的问题,但是,其中很多都在 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 Delegate 的一部分:

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)
    }
}

非常感谢任何帮助,我已经坚持了几个小时,我真的很想了解我做错了什么。

干杯

最佳答案

问题

  • 当您编写 let delegate = AppDelegate() 时。这意味着您创建了一个新的 AppDelegate。您不使用当前的 AppDelegate,而是使用另一个 AppDelegate。这就是为什么 setupTabBarController 方法不会影响任何东西。

  • handleSignOutButtonTapped 结束时调用 setupTabBarController 不是一个好主意。因为它会将当前的 rootViewController 替换为 MainTabBarControllerUINavigation

回答

  • 使用 self.tabBarController? 而不是 self 来呈现 welcomeNavCon
  • 不要在 handleSignOutButtonTapped 方法结束时调用 setupTabBarController
  • MainTabBarController 重新创建并设置新的 viewControllers

代码

@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]) {
  }
}

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  var tabBarController : UITabBarController?


  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
  }

  func resetTabBarController() -> Void {
    let viewControllerAtIndex1 = ...
    let viewControllerAtIndex2 = ...
    let viewControllerAtIndex3 = ...

    tabBarController?.viewControllers = [viewControllerAtIndex1, viewControllerAtIndex2, viewControllerAtIndex3];
  }

}

关于ios - 注销并重置 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594962/

相关文章:

c# - Xamarin App 仅在 Debug模式下崩溃

ios - 如何以编程方式使用 FFMpeg 修剪文件? (libavformat, avutils, ...)

ios - 如何在单个循环中裁剪和保存多个 UIImages

swift - 无法将 xml 转换为对象 - Swift4

javascript - 如何从 Firebase 实时数据库中删除数据(使用 prevChildKey)?

ios - 通过 Firebase Storage 上传图片,让任何人都可以通过公共(public)链接访问

ios - iPA 构建失败,并出现异常 - 为 QA 测试/AdHod 准备构建时出现 NonZeroExcitException

ios - 使用 'YoutubePlayer Swift SDK' 在 tableviewCell 中播放 youtube 视频

swift - 当我打开 UIPickerView 时出现的项目不被视为 "selected"

ios10、Swift 3 和 Firebase 推送通知 (FCM)