override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "ChosenCell" {
if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) {
reference.child("List").child(storyArray[indexPath.row].name).observeSingleEvent(of: .childAdded) { snapshot in
let snapshotValue = snapshot.value as! Dictionary<String,String>
if snapshotValue["Creator"]! != self.currentUser!.email! {
//return false
//continue
} else {
let alert = UIAlertController(title: "Warning", message: "Enter password:", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { alertAction in
let textField = alert.textFields![0] as UITextField
if textField.text! != snapshotValue["Password"]! {
//return false
}
}
alert.addTextField { textField in
textField.placeholder = "password"
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
}
}
return true
}
当用户选择单元格时,如果用户是currentUser,或者没有密码&#34; ChosenCell&#34; segue应该表演。 否则警告:&#34;请输入密码&#34;。如果密码是正确的&#34; ChosenCell&#34; segue应该表演。 我怎么能这样做?
答案 0 :(得分:0)
在这种情况下,func shouldPerformSegue始终返回true,因为observeSingleEvent在后台线程中执行,而shouldPerformSegue不等待observeSingleEvent的完成。 您应该以这种方式实现逻辑:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let name = storyArray[indexPath.row].name
reference.child("List").child(name).observeSingleEvent(of: .childAdded) { snapshot in
let snapshotValue = snapshot.value as! [String : String]
if snapshotValue["Creator"]! == self.currentUser!.email! { // user is the creator of this cell, so perform segue
performSegue(withIdentifier: "ChosenCell", sender: nil)
} else { // user is not the creator of this cell
let alert = UIAlertController(title: "Warning", message: "Enter password:", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { alertAction in
let textField = alert.textFields!.first
if textField.text! != snapshotValue["Password"]! { // wrong password
// show error message
} else { // right password
performSegue(withIdentifier: "ChosenCell", sender: nil)
}
}
alert.addTextField { textField in
textField.placeholder = "password"
}
alert.addAction(action)
present(alert, animated: true)
}
}
}