[英]Performing Segue after UIAlert
我試圖在確認警報后移至下一個ViewController。
@IBAction func yesBtn(_ sender: Any) {
let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure?", preferredStyle: .alert)
let ok = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
self.saveRecord(Answer: "yes")
CATransaction.setCompletionBlock({
self.performSegue(withIdentifier: "mainUse", sender: nil)
})
})
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
print("Cancel button tapped")
}
//Add OK and Cancel button to dialog message
dialogMessage.addAction(ok)
dialogMessage.addAction(cancel)
// Present dialog message to user
self.present(dialogMessage, animated: true, completion: nil)
}
我已在情節提要上添加了segue之后設法執行了segue,但是這會執行兩次segue,一次是在按下“是”按鈕時,另一次是在警報框中確認時。 如果我刪除情節提要上的segue,則根本不會執行segue。 我還嘗試通過將按鈕拖動到下一個視圖控制器,然后選擇custom
而不是show
來創建自定義序列,但這會產生SIGABRT
錯誤。 顯然,在警報框中按確認后,它只能進行一次檢測。
我在網上發現了類似的問題,但大多數似乎都錯過了情節提要的一部分,我應該在兩個視圖之間建立鏈接還是應該以編程方式全部完成? 如果僅以編程方式完成,我應該如何識別下一個視圖控制器?
您可以通過編程來實現。 在這種情況下,無需在視圖控制器之間放置鏈接。
let ok = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
let vc = self.storyboard.instan... //get the destination view controller
self.navigationController.push(vc, animated: true) //or you can present it using self.present(...) method
})
如果僅以編程方式完成,我應該如何識別下一個視圖控制器?
當您從一個屏幕移動到另一個屏幕時,總是會有您要移動到的目標視圖控制器。 因此,您需要使用
self.storyboard.instantia(...)
方法從情節self.storyboard.instantia(...)
獲取該視圖控制器
如何從情節提要中獲取ViewController?
您可以按照以下方式進行
let destVC = UIStoryboard(name: "storyboard_name", bundle: nil).instantiateViewController(withIdentifier: "viewcontollerName_as_set_in_storyboard") as! DestinationViewController
如何設置情節提要ID以查看控制器?
為什么需要CATransaction
? 您不是自己制作動畫。 您不需要CATransaction
,也不應setCompletionBlock
。
警報和segue都有自己的動畫。 我懷疑您的完成塊是在由它們觸發的多個動畫完成時觸發的。
如果僅要確保在主隊列上執行操作,則可以改用DispatchQueue.main.async
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.