Ios 未调用UIAlertAction处理程序
我有一个UITableView,在委托(视图控制器)中,我实现了该功能Ios 未调用UIAlertAction处理程序,ios,swift,uialertcontroller,uialertaction,Ios,Swift,Uialertcontroller,Uialertaction,我有一个UITableView,在委托(视图控制器)中,我实现了该功能 tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 然后我测试编辑风格 if editingStyle == UITableViewCellEditingStyle.Delete {
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语句没有控制台输出,并且肯定没有其他任何事情发生。“是”时不会执行我的删除代码,“取消”时不会调用编辑重置
我在应用程序中的其他TableView上也有相同的设置,它们可以正常工作
这是在一个视图控制器中,该视图控制器是从另一个视图控制器(如果该视图控制器具有任何方向)以模态方式呈现的。对于正在进行的任何其他演示,我没有收到任何错误(因此,if self.presentedViewController==nil
块)
我显然在某个地方出了问题,但目前我看不出哪里出了问题。有什么想法吗
8.4中使用的IOS版本。目标是iPad。您可以检查这是否有效
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")
}
}))
检查您的
ViewController
是否是导航的childViewController
。如果导航覆盖:
(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^) (void))completion;
您必须确保调用super dismissViewControllerAnimated:flag completion:completion
并确保参数completion
不能通过nil
UIAlertController
将调用此方法(我也混淆了这一点)。
我注销调用方是UIAlertController\u dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:
(再次混淆)
这对我有用。我希望这个问题对你也有用。这个问题很老了,但我今天遇到了同样的麻烦 关键是你正在使用iPad,所以你必须在popover中展示它
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: {})
}
如果UIDevice.current.userInterfaceIdiom=.pad{
alert.modalPresentationStyle=.popover
设pp=ac.popoverPresentationController
pp?.sourceView=发送方为?UIView//或pp?.sourceRect=
当前(警报,动画:真,完成:{})
}否则{
当前(警报,动画:真,完成:{})
}
注意:它是swift3我也遇到了同样的问题,我发现我覆盖了disclose函数:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: nil)
}
当您传递nil时,UIAlertController正在使用完成块传递数据。操作根本不调用
当您重写disclose func时,需要传递completion参数
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: completion)
}
希望我能帮你顺便提一下,delete工具很有效。如果我不费心尝试提示,我的删除代码将按预期工作。我只是不知道我在UIAlertController和UIAlertActions上做错了什么。我使用了你们的代码,它工作得很好。是的,它在我的应用程序的其他领域也工作得很好-所以我很困惑为什么它在这个瞬间不工作不,这不工作。未调用处理程序。警报出现时,“取消”操作出现在警报上。点击取消-警报被解除,处理程序不被调用。同上。所以“取消”不打印?也没有在处理程序中调用断点?这正是发生的情况。我不明白为什么没有呼叫处理程序。目前,我只是在不提示用户的情况下执行删除。我就是搞不懂。对于同一应用程序中的类似提示,我在其他地方使用了相同的代码,甚至对于其他TableView中的删除。我只是看不出这里有什么不同。我甚至粘贴了来自其他区域的相同代码,只更改了主警报消息的文本,处理程序中只有一条println语句。没有调用处理程序。应用程序中的任何其他警报都不需要popover。这使我免于拔出发根。谢谢你是我的人!确保为swizzled方法调用completion,以防在那里运行自己的块