未调用 UIAlertAction 处理程序 [英] UIAlertAction handler is not called

查看:28
本文介绍了未调用 UIAlertAction 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 并且在委托(视图控制器)中,我已经实现了这个功能

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

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

然后我测试编辑风格

    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.

出现带有标题、消息和按钮取消"和是"的警报.如果我点击任何一个,都不会发生任何事情.警报被解除, println 语句没有控制台输出,并且肯定没有其他事情发生.我的删除代码不会为是"执行,并且不会为取消"调用编辑重置.

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".

我在应用程序中的其他 tableview 上也有同样的设置,它们按预期工作.

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

这是在从另一个视图控制器模态呈现的视图控制器中(如果有任何意义).我没有收到关于任何其他演示的任何错误(因此 if self.presentedViewController == nil 块).

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 版本在 8.4 中使用.目标是 iPad.

IOS version being used in 8.4. Target is iPad.

推荐答案

我遇到了同样的问题,我发现我覆盖了dismiss func:

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

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

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

当你覆盖dismiss func时你需要传递completion参数

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

希望能帮到你

这篇关于未调用 UIAlertAction 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆