Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertAction handler is not called

I have a UITableView and in the delegate (view controller), I have implemented the function

    tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

I then test for the editing style

    if editingStyle == UITableViewCellEditingStyle.Delete {
        // do deleting stuff here
    }

As part of the delete, I am requesting confirmation from the user, if they select "yes", the item related to the row will be deleted, if they select no, I reset the editing style.

  var alert = UIAlertController(title: "Delete Item", message: "Are you sure you want to delete the selected item?", preferredStyle: UIAlertControllerStyle.Alert)

  //delete
  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive, handler: { (action: UIAlertAction!) -> Void in
      println("Yes action was selected")
      //delete my object and remove from the table
  }))

  //cancel
  alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (action: UIAlertAction!) -> Void in
      //reset editing
      println("Cancel action was selected")
      self.tableView.setEditing(false, animated: true)
  }))

  if self.presentedViewController == nil {
      presentViewController(alert, animated: true, completion: nil)
  }

The problem I appear to have is that neither completion handler is being called. I've used this format elsewhere with no issues.

The alert appears with the title, message and the buttons "Cancel" and "Yes". If I tap on either, nothing happens. The alert is dismissed and there are is no console output for the println statements and there is definitely nothing else happening. My delete code isn't executed for "Yes" and the editing reset isn't called for "cancel".

I have this same setup on other tableviews within the application and they work as expected.

This is in a view controller that has been presented modally from another view controller (if that has any bearing). I'm not getting any errors about any other presenting going on (hence the if self.presentedViewController == nil block).

I've obviously gone wrong somewhere, but at the moment I just can't see where. Any ideas?

IOS version being used in 8.4. Target is iPad.

like image 749
jpmcc Avatar asked Jul 29 '15 09:07

jpmcc


2 Answers

I had the same issue and I found that I override the dismiss func:

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    super.dismiss(animated: true, completion: nil)
}

the UIAlertController is using the completion block to pass data when you pass nil the action doesn't call at all.

When you override the dismiss func you need to pass the completion parameter

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    super.dismiss(animated: true, completion: completion)
}

hope I help

like image 87
Yair hadad Avatar answered Nov 12 '22 05:11

Yair hadad


Check your ViewController is a childViewController of navigation. And if the navigation override:

(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^) (void))completion;

Your must sure call super dismissViewControllerAnimated:flag completion:completion.

And ensure parameter completion can not pass nil. UIAlertController will call this method (I confused that too). I log out the caller is UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView: (confused again).

It work for me. I hope it work for your too.

like image 5
sam chi wen Avatar answered Nov 12 '22 04:11

sam chi wen