【发布时间】:2015-07-29 09:38:01
【问题描述】:
我有一个 UITableView 并且在委托(视图控制器)中,我已经实现了该功能
tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
然后我测试编辑风格
if editingStyle == UITableViewCellEditingStyle.Delete {
// do deleting stuff here
}
作为删除的一部分,我要求用户确认,如果他们选择“是”,与该行相关的项目将被删除,如果他们选择“否”,我会重置编辑样式。
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)
}
我似乎遇到的问题是两个完成处理程序都没有被调用。我在其他地方使用过这种格式,没有任何问题。
警报会显示标题、消息以及“取消”和“是”按钮。如果我点击任何一个,都不会发生任何事情。警报被解除,并且 println 语句没有控制台输出,并且肯定没有其他任何事情发生。 “是”没有执行我的删除代码,“取消”没有调用编辑重置。
我在应用程序中的其他表视图上也有相同的设置,它们按预期工作。
这是在一个视图控制器中,该视图控制器已从另一个视图控制器模态呈现(如果有任何影响)。我没有收到关于正在进行的任何其他演示的任何错误(因此if self.presentedViewController == nil 块)。
我显然在某个地方出错了,但目前我只是看不出在哪里。有什么想法吗?
IOS 版本在 8.4 中使用。目标是 iPad。
【问题讨论】:
-
顺便提一下,删除的东西是有效的。如果我不费心尝试提示,我的删除代码将按预期工作。我只是看不出我在 UIAlertController 和 UIAlertActions 上做错了什么。
-
我使用你的代码,它运行良好。
-
是的,它在我的应用程序的其他区域也可以完美运行 - 因此对于为什么它在这种情况下不起作用感到困惑
标签: ios swift uialertcontroller uialertaction