Ios 在UIAlert之后执行Segue
在确认警报后,我正尝试移动到下一个ViewControllerIos 在UIAlert之后执行Segue,ios,swift,Ios,Swift,在确认警报后,我正尝试移动到下一个ViewController @IBAction func yesBtn(_ sender: Any) { let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure?", preferredStyle: .alert) let ok = UIAlertAction(title: "Confirm", style: .default, handle
@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,一次是在按下yes按钮时,另一次是在警报框中确认时。如果我在故事板上删除了这个片段,那么这个片段就根本不会被执行。我还尝试通过将按钮拖动到下一个视图控制器,然后选择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(…)
方法从故事板获取视图控制器
如何从情节提要获取viewcontroller?
你可以用下面的方法
let destVC = UIStoryboard(name: "storyboard_name", bundle: nil).instantiateViewController(withIdentifier: "viewcontollerName_as_set_in_storyboard") as! DestinationViewController
如何设置脚本id以查看控制器?
为什么需要
CATTransaction
?你不是在自己做动画。您不需要CATTransaction
,也不应该setCompletionBlock
alert和segue都有自己的动画。我怀疑你的完成块是在完成由它们触发的多个动画时触发的
如果您只想确保在主队列上执行操作,可以使用
DispatchQueue.main.async
确保您的yesBtn
在segue中直接连接