Is there an easy way to log out when I click on the rear navigation item on the navigation controller in quick mode 4?

I have a login screen that has an email address and password. I am using firebase Auth.

The login screen is integrated into the navigation controller. On the login screen, it goes to UserDetailsController. The navigation bar has a β€œrear” navigation element that comes with the navigation controller. I can't drag this to be the way out.

I was wondering if there is an easy way when the β€œreverse” is pressed and the user returns to the login page to log out. The code to exit the system is relatively simple using Firebase Auth. The problem I am running into works in LoginController if I return here from UserDetailsController.

I read about using self.presentingcontroller to determine which controller I came back from, but I keep getting zero. And I was not sure if this is the best / only option for determining which controller I came back from.

Thanks.

+5
source share
2 answers

It looks pretty simple. Hope this code helps.

  • In viewDidLoad add the following function:

    override func viewDidLoad() { super.viewDidLoad() setupLogOut() } 
  • Then configure a function like this using the selector handler method, for example:

     fileprivate func setupLogOut() { navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(handleLogOut)) } @objc func handleLogOut() { do { try Auth.auth().signOut() // Present the login controller let loginController = LoginController() let navController = UINavigationController(rootViewController: loginController) self.present(navController, animated: true, completion: nil) } catch let signOutErr { print("Failed to sign out:", signOutErr) } } 
+5
source

If you do not want to add the method button back. You can also use the method below to log out:

 override func viewWillDisappear(animated: Bool) { // Your sign out logic here.. } 

The method call is called when you return to the input controller.

+1
source

Source: https://habr.com/ru/post/1272622/


All Articles