UIAlertAction handler is not called

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

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

      

Then I test the editing style

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

      

As part of the deletion, I am asking for confirmation from the user, if they select yes, the item associated with that line will be deleted, if they did not select, I reset the edit 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'm running into is that no completion handler is being called. I've used this format elsewhere with no problem.

A warning appears with a title, message, and Cancel and Yes buttons. If I touch anyone, nothing happens. The warning is dismissed and there is no console output for the println commands, and certainly nothing else happens. My delete code fails for "Yes" and edit reset is not called for "undo".

I have the same setup on other table views in the application and they work as expected.

This is in a view controller that was presented modally from another view controller (if it has any notification). I am not getting any errors regarding any other presentations (hence the block if self.presentedViewController == nil

).

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

The IOS version is used in 8.4. Target - iPad.

+3


source to share


4 answers


Check out what ViewController

is childViewController

navigation. And if overriding navigation:

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

      

Mandatory call super dismissViewControllerAnimated:flag completion:completion

.



And make sure the parameter completion

cannot pass nil

. UIAlertController

will call this method (I was confused too). I log out of the caller UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:

(confused again).

This works for me. I hope this works for you too.

+1


source


you can check if this works



    alert.addAction(UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: { action in
        switch action.style{
        case .Default:
            println("default")

        case .Cancel:
            println("cancel")

        case .Destructive:
            println("destructive")
        }
    }))

      

0


source


This question is quite old, but I faced the same problem today.

The point is that you are using an iPad to present it in a popover

if UIDevice.current.userInterfaceIdiom == .pad{
  alert.modalPresentationStyle = .popover
  let pp = ac.popoverPresentationController
  pp?.sourceView = sender as? UIView // or pp?.sourceRect = <some rect...>
  present(alert, animated: true, completion: {})
}else{
  present(alert, animated: true, completion: {})
}

      

Note. This is swift3

0


source


I had the same problem and found myself overriding the firing function:

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

      

UIAlertController uses a completion block to pass data, when you pass null, the action is not called at all.

When you override the fire function, 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

0


source







All Articles