【问题标题】:Unable to put two AlertControllers on same VC无法将两个 AlertControllers 放在同一个 VC 上
【发布时间】:2016-01-08 04:56:44
【问题描述】:

我有一个带有项目集合视图的屏幕。如果用户没有选择任何东西,我希望弹出一个警报,提示他们选择一些东西。如果他们选择了什么,我想弹出一个警报,询问他们是否准备好继续前进?下面是我的代码:

   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

这似乎应该很容易工作并且是一件很常见的事情,但是没有在线解决这个问题。任何帮助/指导将不胜感激。

【问题讨论】:

    标签: ios swift uiviewcontroller swift2 uialertcontroller


    【解决方案1】:

    您错过了alert2

     self.presentViewController(alert2, animated: true, completion: nil)
    

    添加它,它会正常工作。

    【讨论】:

      【解决方案2】:

      此行不是必需的:

      alert.dismissViewControllerAnimated(true, completion: nil)

      同时添加

          self.presentViewController(alert, animated: true, completion: nil)
      

      在其他部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 2020-03-15
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        • 2018-02-27
        • 1970-01-01
        相关资源
        最近更新 更多