Swift - 在 prepareForSegue 方法中显示警报时发出警告
Swift - Warning when presenting alert inside prepareForSegue method
当我显示 UIAlertController 时,我不断收到以下警告:
2016-08-16 13:29:48.138 MyProject[602:98207] pushViewController:animated: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.
这里是相关的一段代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toUserData" && didSelect
{
let vc:UserDataViewController = segue.destinationViewController as! UserDataViewController
vc.unitIndex = self.selectedIndex - 1
}
else
{
self.showAlert()
return
}
}
func showAlert()
{
let alert = UIAlertController(title: "Error", message: "Are you sure that you set units properly?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true, completion: nil)
}
}
我以前在其他项目中使用过这种结构,我不记得有过这样的警告。
此外,我真的不知道为什么会收到它,因为显示警报不会中断任何其他有关转换的操作。
提前致谢
我使用了错误的方法来检查是否一切都已准备就绪以执行 segue,这种事情应该使用 shouldPerformSegueWithIdentifier
方法来完成:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == "toUserData" && didSelect
{
return true
}
else
{
self.showAlert()
return false
}
}
当我显示 UIAlertController 时,我不断收到以下警告:
2016-08-16 13:29:48.138 MyProject[602:98207] pushViewController:animated: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.
这里是相关的一段代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toUserData" && didSelect
{
let vc:UserDataViewController = segue.destinationViewController as! UserDataViewController
vc.unitIndex = self.selectedIndex - 1
}
else
{
self.showAlert()
return
}
}
func showAlert()
{
let alert = UIAlertController(title: "Error", message: "Are you sure that you set units properly?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true, completion: nil)
}
}
我以前在其他项目中使用过这种结构,我不记得有过这样的警告。
此外,我真的不知道为什么会收到它,因为显示警报不会中断任何其他有关转换的操作。
提前致谢
我使用了错误的方法来检查是否一切都已准备就绪以执行 segue,这种事情应该使用 shouldPerformSegueWithIdentifier
方法来完成:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == "toUserData" && didSelect
{
return true
}
else
{
self.showAlert()
return false
}
}