我有一个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)
}我似乎遇到的问题是,两个完成处理程序都没有被调用。我在其他地方使用过这种格式,没有任何问题。
将出现带有标题、消息和按钮"Cancel“和"Yes”的警报。如果我点击其中任何一个,都不会有任何反应。警报被解除,println语句没有控制台输出,并且肯定没有发生任何其他事情。我的删除代码没有为"Yes“执行,编辑重置也没有为"cancel”调用。
我在应用程序中的其他表视图上也有相同的设置,它们可以按预期工作。
这是在一个视图控制器中,该视图控制器是从另一个视图控制器(如果有任何影响)以模态方式呈现的。我没有收到任何关于任何其他演示正在进行的错误(因此出现了if self.presentedViewController == nil块)。
很明显,我在什么地方出错了,但目前我就是看不到哪里。有什么想法吗?
8.4中使用的IOS版本。目标是iPad。
发布于 2018-01-21 22:42:13
我也遇到了同样的问题,我发现我覆盖了dismiss函数:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: nil)
}UIAlertController正在使用完成块来传递数据,当您传递nil时,操作根本不会调用。
当你重写dismiss函数时,你需要传递完成参数
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: completion)
}希望我能帮上忙
发布于 2017-01-17 14:00:40
检查您的ViewController是否为导航的childViewController。如果导航覆盖:
(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^) (void))completion;你一定要给super dismissViewControllerAnimated:flag completion:completion打电话。
并确保参数completion不能通过nil。UIAlertController会调用这个方法(我也搞混了)。我注销了呼叫者是UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView: (再次困惑)。
这对我很管用。我希望它也适用于你。
发布于 2015-07-29 17:57:05
您可以检查这是否正常工作
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")
}
}))https://stackoverflow.com/questions/31696643
复制相似问题