我有一个带有项目 Collection View 的屏幕。如果用户没有选择任何东西,我想弹出一个警告,提示他们选择一些东西。如果他们选择了某些东西,我想弹出一个警报,询问他们是否准备好继续前进?下面是我的代码:
if (isSelected) {
// create the alert
let alert = UIAlertController(title: "Create", message: "Make sure to select at least one item.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { action in
alert.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let alert2 = UIAlertController(title: "Move on", message: "Are you ready to move on?", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { action in
self.performSegue to next screen
}))
n.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: { action in
}))
}
代码似乎没问题,但出现以下错误:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
这看起来应该很容易工作并且很常见,但是这个问题没有在线解决方案。非常感谢任何帮助/指导。
最佳答案
您错过了展示alert2
self.presentViewController(alert2, animated: true, completion: nil)
添加它,它将正常工作。
关于ios - 无法将两个 AlertController 放在同一个 VC 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34669782/