[英]UIAlertAction handler is not called
我有一個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語句沒有控制台輸出,並且肯定沒有其他事情發生。 我的刪除代碼沒有執行“是”,並且沒有為“取消”調用編輯重置。
我在應用程序中的其他tableviews上有相同的設置,它們按預期工作。
這是一個視圖控制器,它是從另一個視圖控制器以模態方式呈現的(如果有任何方位)。 我沒有得到任何其他正在進行的任何錯誤(因此if self.presentedViewController == nil
塊)。
我顯然在某個地方出了問題,但此刻我無法看到哪里。 有任何想法嗎?
在8.4中使用IOS版本。 目標是iPad。
檢查你的ViewController
是一個導航的childViewController
。 如果導航覆蓋:
(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^) (void))completion;
你一定要調用super dismissViewControllerAnimated:flag completion:completion
。
並確保參數completion
不能通過nil
。 UIAlertController
將調用此方法(我也很困惑)。 我注銷調用者是UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:
再次混淆)。
它對我有用。 我希望它對你也有用。
我有同樣的問題,我發現我覆蓋了dismiss func:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: nil)
}
UIAlertController在傳遞nil時使用完成塊傳遞數據,操作根本不調用。
當您覆蓋dismiss func時,您需要傳遞完成參數
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: true, completion: completion)
}
希望我能幫忙
你可以檢查一下這是否有效
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")
}
}))
這個問題相當陳舊,但我今天遇到了同樣的麻煩。
關鍵是你正在使用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: {})
}
注意:這是swift3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.