我有一个带有项目集合视图的屏幕。如果用户没有选择任何内容,我想要弹出警报,提示他们选择一些内容。如果他们选择了某些东西,我想要弹出警报,询问他们是否已准备好继续前进?以下是我的代码:
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
这看起来应该很容易并且很常见,但是没有解决这个问题的方法。任何帮助/指导将不胜感激。
答案 0 :(得分:3)
您错过了 alert2
self.presentViewController(alert2, animated: true, completion: nil)
添加它,它会正常工作。
答案 1 :(得分:0)
此行不是必需的:
alert.dismissViewControllerAnimated(true,completion:nil)
同时添加
self.presentViewController(alert, animated: true, completion: nil)
in else part。