当我对注销按钮进行编程时,当它单独存在而不是在单元格中时,它工作正常。但是,我遇到了这个问题,我通过故事板创建了一个表格视图,然后向其中添加了一些单元格,然后将相同的注销按钮添加到单元格并创建了 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
答案 0 :(得分:0)
我认为在您的情况下最好的做法是委托回调。
你必须定义一个协议并在你的视图控制器中实现它
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 上,您只需调用委托方法。
class SettingsTableViewCell {
var loginDelegate: LoginDelegate?
@IBAction func logUserOut(_ sender: Any) {
// Callback to the delegate
self.loginDelegate?.didPressLogOut()
}
}