Reputation: 567
I've searched similar answers in how to displace an UIAlertController
in a TableViewCell
via protocols
and delegates
. I got that part working. But I got two actions in the UIAlertController
. One of them confirms an action and returns true, and the other button is cancel and returns false.
I can successfully present the UIAlertController
, but the program just runs through my code and ignore the logic in my confirm button. I think this is because the delegate/protocol method runs the delegate
method in parallel/asynchronously and doesn't wait for the return value.
Here is my code in my custom TableViewCell:
var unattend: Bool!
let customAlert = UIAlertController(title: "Not going anymore?", message: "Event will disappear from this list, confirm?", preferredStyle: UIAlertControllerStyle.Alert)
let nevermindAction = UIAlertAction(title: "Nevermind", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in
unattend = false
}
let unattendAction = UIAlertAction(title: "Confirm unattending", style: UIAlertActionStyle.Destructive) { (action: UIAlertAction) -> Void in
unattend = true
}
customAlert.addAction(nevermindAction)
customAlert.addAction(unattendAction)
self.delegate.goingCancelled(customAlert, unattend: unattend))
Here is my protocol:
protocol GoingCancelledDelegate {
func goingCancelled(alert: UIAlertController) -> Bool
}
Here is my code in my TableViewController, and yes I did type in the GoingCancelledDelegate in the class definition just didn't copy and paste here:
func goingCancelled(alert: UIAlertController) -> Bool {
presentViewController(alert, animated: true) { () -> Void in
}
}
So the alert shows up, but my UITableViewCell
code doesn't wait for the return value to process other logic. Any tips/advice/help will be greatly appreciated! Thank you in advance!
Upvotes: 0
Views: 68
Reputation: 3084
Its because you are calling self.delegate.goingCancelled(customAlert, unattend: unattend))
without confirm action do this:
var unattend: Bool!
let customAlert = UIAlertController(title: "Not going anymore?", message: "Event will disappear from this list, confirm?", preferredStyle: UIAlertControllerStyle.Alert)
let nevermindAction = UIAlertAction(title: "Nevermind", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in
unattend = false
}
let unattendAction = UIAlertAction(title: "Confirm unattending", style: UIAlertActionStyle.Destructive) { (action: UIAlertAction) -> Void in
unattend = true
self.delegate.goingCancelled(customAlert, unattend: unattend))
}
customAlert.addAction(nevermindAction)
customAlert.addAction(unattendAction)
Upvotes: 1