从单元注销后访问登录屏幕视图控制器



当我对注销按钮进行编程时,当它单独运行而不在单元格中时,它工作得很好。然而,我遇到了这个问题,我通过情节提要创建了一个表视图,然后向其中添加了一些单元格,然后在单元格中添加了相同的注销按钮,并创建了SettingsTableViewCell.Swift文件,这是一个单元格文件,而不是视图控制器。因此,我以前使用的代码不能作为单元格和视图控制器使用。我找不到能做与以前相同事情的代码,只是从一个单元格中找到。

当按钮在视图控制器文件中时,我可以注销,如果注销成功,则将用户发送到登录屏幕,如果用户再次登录,则将其返回主页(而不是设置(。我想编码一些做完全相同事情的东西,但来自一个单元格。

这是我之前在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和navigationController不是UITableViewCell的一部分,因此我无法使用此代码。

如果你想看看AuthManager.shared.logOut中有什么,下面是它的代码:

// 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

我认为在您的情况下最好的做法是委托回调。

你必须定义一个协议,并在你的视图控制器中实现它

protocol LoginDelegate {

func didPressLogOut()
}
class SettingsViewController : LoginDelegate {

func didPressLogOut() {
// 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)
}
}

在视图控制器中初始化自定义单元格时,请确保看到视图控制器的委托

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsTableViewCellIdentifier", for: indexPath) as! SettingsTableViewCell
cell.loginDelegate = self
// Rest of the cell setup code
return cell
}

然后在你的单元格类中,在IBAction上,你只需要调用delegate方法。

class SettingsTableViewCell {

var loginDelegate: LoginDelegate?

@IBAction func logUserOut(_ sender: Any) {
// Callback to the delegate
self.loginDelegate?.didPressLogOut()
}
}

最新更新