10

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.

4

4 回答 4

6

我有同样的问题,我发现我覆盖了解雇函数:

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

当您传递 nil 时,UIAlertController 正在使用完成块传递数据,该操作根本不会调用。

当您覆盖关闭函数时,您需要传递完成参数

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

希望我能帮上忙

于 2018-01-21T14:42:12.827 回答
5

检查您ViewControllerchildViewController导航。如果导航覆盖:

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

你一定要打电话super dismissViewControllerAnimated:flag completion:completion

并确保参数completion不能通过nilUIAlertController将调用此方法(我也很困惑)。我注销来电者UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:(再次感到困惑)。

它对我有用。我希望它也适用于你。

于 2017-01-17T06:00:39.623 回答
0

你可以检查这是否有效

    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")
        }
    }))
于 2015-07-29T09:57:04.743 回答
0

这个问题很老了,但我今天遇到了同样的麻烦。

关键是您使用的是 iPad,因此您必须在弹出窗口中显示它

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: {})
}

注意:这是swift3

于 2017-04-30T02:14:48.823 回答