Ios 使用完成块连续显示两个UIAlertController对象
我试图连续显示两个UIAlertController实例,就像下面的代码块一样Ios 使用完成块连续显示两个UIAlertController对象,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我试图连续显示两个UIAlertController实例,就像下面的代码块一样 func showAlerts() { let alertA = UIAlertController(title: "Alert A", message: "This is alert a...", preferredStyle: .alert) let alertB = UIAlertController(title: "Alert B", message: "This is alert b..."
func showAlerts() {
let alertA = UIAlertController(title: "Alert A", message: "This is alert a...", preferredStyle: .alert)
let alertB = UIAlertController(title: "Alert B", message: "This is alert b...", preferredStyle: .alert)
let alertButton1 = UIAlertAction(title: "OK", style: .default, handler: nil)
let alertButton2 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertA.addAction(alertButton1)
alertA.addAction(alertButton2)
alertB.addAction(alertButton1)
alertB.addAction(alertButton2)
self.present(alertA, animated: true) {
self.present(alertB, animated: true, completion: {
debugPrint("alerts are all shown")
})
}
}
我希望此代码连续显示每个警报,这意味着alertB
在alertA
之后显示。但alertB
并不像我预期的那样出现,控制台上的警告是:
Warning: Attempt to present <UIAlertController: 0x7f7ffde0ace0> on <ContinuousUIAlertController_Experiment.ViewController: 0x7f7ffdd092d0> which is already presenting <UIAlertController: 0x7f7ffde09f90>
警告:尝试呈现已呈现的内容
如果我没记错的话,多个UIAlertController对象不能同时存在。所以我知道上面的警告告诉了我什么
那么,我如何使用UIViewController::present(\uu:animated:completion:)
的完成或使用几乎相同的逻辑实现连续警报显示呢?(我不喜欢使用UIAlertAction的处理程序)
如果有解决方案,请告诉我。我为这个问题挣扎了几天,但还没有解决
谢谢,您不能一次显示2 uialettcontrol 但您可以在第一个UIALETControl的UIALETraction上显示第二个UIALETControl 比如说
let alertController = UIAlertController(title: "iOScreator", message:
"Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
self.pressed()
}))
func pressed()
{
print("you pressed")
}
在按下事件时,您可以为第二个uialettcontrol编写代码
希望这对你有帮助
祝您编码愉快。您不能同时显示2 uialettcontrol 但您可以在第一个UIALETControl的UIALETraction上显示第二个UIALETControl 比如说
let alertController = UIAlertController(title: "iOScreator", message:
"Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
self.pressed()
}))
func pressed()
{
print("you pressed")
}
在按下事件时,您可以为第二个uialettcontrol编写代码
希望这对你有帮助
祝您编码愉快。同时显示两个警报有什么意义?用户将只能看到一个。为什么不在用户解除第一个警报后显示第二个呢?您可以使用按钮将多个自定义视图添加为子视图。@rmaddy连续显示两个警报,而不是同时显示。是的,我想在第一个警报消失后显示第二个警报,不知何故,使用完成块。@iAviator您能解释更多细节吗?如果有代码示例,我会毕业的!您的代码不会试图一个接一个、一遍又一遍地显示警报。它只尝试同时显示这两个警报,而且只显示一次。同时显示两个警报有什么意义?用户将只能看到一个。为什么不在用户解除第一个警报后显示第二个呢?您可以使用按钮将多个自定义视图添加为子视图。@rmaddy连续显示两个警报,而不是同时显示。是的,我想在第一个警报消失后显示第二个警报,不知何故,使用完成块。@iAviator您能解释更多细节吗?如果有代码示例,我会毕业的!您的代码不会试图一个接一个、一遍又一遍地显示警报。它只尝试同时显示这两个选项,并且只显示一次。这应该是可行的,但是OP提到不要使用UIAlertAction的Handler谢谢你的回答。它实际上是有效的,但正如@ronatory所提到的,我不喜欢使用UIAlertAction的处理程序,例如完成
UIViewController::present(quot:animated:completion:)
。您还有其他解决方案吗?然后,请分享。这应该是可行的,但是OP提到不要使用UIAlertAction的手柄。谢谢你的回答。它实际上是有效的,但正如@ronatory所提到的,我不喜欢使用UIAlertAction的处理程序,例如完成UIViewController::present(quot:animated:completion:)
。您还有其他解决方案吗?然后请分享。