[英]How to dismiss modal ViewController from UIAlertcontroller
我展示了一個模態viewcontroller
,用戶可以在其上決定編輯或刪除所展示的汽車。
如果用戶想刪除這輛車,我會提供一個具有警報樣式的UIAlertController
,詢問他是否真的要刪除這輛車。 一切正常。 但是在用戶選擇“ 是 ”之后,我仍然處於modal viewcontroller
。
刪除后如何關閉模態視圖?
我嘗試了以下代碼
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
和
self.navigationController?.popViewControllerAnimated(true)
在“確定行動”的結局中,但兩者都不對我有用。 :(
沒有理由在其中編寫代碼
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
上面的代碼用於在主線程中工作。 但是這里您已經在主線程上了。
這里的問題是你在打電話
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
而不是寫
self.dismissViewControllerAnimated(true, completion: nil)
因為您要在self
控制器上顯示AlertController
,所以唯一可以將其關閉的人是self
將您的代碼放入
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
像那樣 :
func showDeleteWarningAndDeleteEntity() {
dispatch_async(dispatch_get_main_queue(), {
let deleteController = UIAlertController(title: "Delete car", message: "Are you sure?", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {
(action) in
}
let okACtion = UIAlertAction(title: "Yes", style: .Destructive) {
(action) in
//some network stuff happens here...
self.dismissViewControllerAnimated(true, completion: nil)
}
deleteController.addAction(okACtion)
deleteController.addAction(cancelAction)
self.presentViewController(deleteController, animated: true, completion: nil)
})
}
希望這對你有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.