When I was programming the logout button, it was working fine when it was just by itself and not in a cell. However, I came across this issue where I created a table view through storyboard and then added some cells to it, I then added that same logout button to the cell and created SettingsTableViewCell.Swift file, which is a cell file and not a view controller. So the code I was using before did not work as a cell and a view controller are not the same thing. And I cannot find the code that does the same things it was doing before but from a cell.
When the button was in the view controller file, I was able to log out, send the user to the login screen if the logout was successful, and then return user to home (not settings) if they login again. I want to code somethings that does the exact same thing, but from a cell.
Here is my previous code in SettingsViewController.Swift:
@IBAction func logUserOut(_ sender: Any) {
// Display an Alert to user to confirm logging out
let actionSheet = UIAlertController(title: "Log Out", message: "Are you sure you want to log out?", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { _ in
AuthManager.shared.logOut(completion: {success in
DispatchQueue.main.async {
if (success) {
// Go to login after logout
let loginVC = self.storyboard?.instantiateViewController(identifier: "login")
loginVC?.modalPresentationStyle = .fullScreen
self.present(loginVC!, animated: true, completion: {
self.navigationController?.popToRootViewController(animated: false)
self.tabBarController?.selectedIndex = 0
})
}
else {
// Error Occurd
fatalError("Could not log out user")
}
}
})
}))
present(actionSheet, animated: true)
} // End logUserOut method
present and navigationController are not part of a UITableViewCell so I cannot use this code.
In case you want to see what is in AuthManager.shared.logOut, here is the code for it:
// Log out user
public func logOut(completion: (Bool) -> Void) {
do {
try Auth.auth().signOut()
completion(true)
return
}
catch {
print(error)
completion(false)
return
}
} // End logOut method