0

I am using below code in shouldPerformSegue. I want to stop or continue segue based on whether user selects delete or cancel.

I noticed that segue stops due to the alert. If I remove alert then segue works fine.

Is shouldPerformSegue incorrect place to use such validation?

Thanks.

let alertController = UIAlertController(title: "Confirm Delete", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.alert)

let deletelAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive) { (result : UIAlertAction) -> Void in
            print("Delete")

    if self.selection != nil {    
        if self.selection!.delete() == true { //this is database call
            self.success =  true
        } else {
            self.success =  false
    }
}         
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
    print("Cancelled delete")
    self.success =   false
}
alertController.addAction(deletelAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)

return success
Nirav D
  • 71,513
  • 12
  • 161
  • 183
ashishn
  • 410
  • 5
  • 18
  • You are setting `success` to `true/false` within closures, so you should use callback instead of a `return` method. – Santosh Sep 27 '16 at 16:36
  • Santosh, I haven't used callbacks so far so need to learn first. Will check out. – ashishn Sep 29 '16 at 03:24

1 Answers1

0

I found the answer thanks to comment by Paulw11 to this question.

So I deleted unwind segue from Delete bar button and added from View Controller to Exit. And then I call this segue programmatically from delete action method. This worked.

however I noticed that shouldPerformSegue was not called. The reason is explained at this link by nburk that shouldPerformSegue will not be called if segue is called programatically. This is really smart of Apple engineers :)

Community
  • 1
  • 1
ashishn
  • 410
  • 5
  • 18