jpm*_*mcc 6 ios swift uialertcontroller uialertaction
我有一个UITableView,在委托(视图控制器)中,我实现了该功能
tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
然后我测试编辑风格
if editingStyle == UITableViewCellEditingStyle.Delete {
// do deleting stuff here
}
Run Code Online (Sandbox Code Playgroud)
作为删除的一部分,我要求用户确认,如果他们选择"是",则与该行相关的项目将被删除,如果他们选择否,则重置编辑样式.
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)
}
Run Code Online (Sandbox Code Playgroud)
我似乎遇到的问题是没有调用完成处理程序.我在其他地方使用过这种格式没有任何问题.
出现警报,标题,消息和"取消"和"是"按钮.如果我点击任何一个,没有任何反应.警报被取消,并且println语句没有控制台输出,并且肯定没有其他事情发生.我的删除代码没有执行"是",并且没有为"取消"调用编辑重置.
我在应用程序中的其他tableviews上有相同的设置,它们按预期工作.
这是一个视图控制器,它是从另一个视图控制器以模态方式呈现的(如果有任何方位).我没有得到任何其他呈现的错误(因此if self.presentedViewController == nil块).
我显然在某个地方出了问题,但此刻我无法看到哪里.有任何想法吗?
在8.4中使用IOS版本.目标是iPad.
我有同样的问题,我发现我覆盖了dismiss func:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
UIAlertController 正在使用完成块来传递数据,当您传递 nil 操作根本不调用时。
当您覆盖dismiss func时,您需要传递完成参数
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: completion)
}
Run Code Online (Sandbox Code Playgroud)
希望我有帮助
检查ViewController是一个childViewController导航。如果导航优先:
(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^) (void))completion;
Run Code Online (Sandbox Code Playgroud)
您一定要致电super dismissViewControllerAnimated:flag completion:completion。
并确保参数completion不能通过nil。UIAlertController将调用此方法(我也感到困惑)。我注销呼叫者是UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:(再次感到困惑)。
它对我有用。我希望它也对您有用。
小智 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")
}
}))
Run Code Online (Sandbox Code Playgroud)