ios - 无法将两个 AlertController 放在同一个 VC 上

标签 ios swift uiviewcontroller swift2 uialertcontroller

我有一个带有项目 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/

相关文章:

android - Qt在不同手机上创建一个文件

ios - Swift & Core Data - 将实体添加到现有实体

cocoa-touch - Storyboard - 容器 View - 根据条件嵌入不同的 View

ios - 使 UIImageView(箭头图像)指向我在屏幕上触摸的位置

iphone - 使用堆栈的导航 Controller / View Controller

ios - 有什么方法可以快速从 AppDelegate 调用 gui 元素吗?

ios - 移动客户端不应该终止与API的连接吗?

ios - 是否可以重定向调用?

ios - setStatusBarHidden 已弃用,但唯一有效的方法

swift - 如何在有顺序的循环中使用DispatchGroup?