试图背靠背呈现 2 个 UIAlertController

Trying to present 2 UIAlertControllers back to back

我有一个 UIAlertController,其中的选项是从一个数组中填充并呈现给用户的。然后用户从警报中选择一个选项。在此之后,我有一个单独的警报,为用户提供带有确定按钮的确认消息。

myAlert.addAction(UIAlertAction.init(title: item, style: .Default, handler: { 
    (UIAlertAction) in
         self.chosenBusiness.append(businessNameData[item]!)
}))
self.presentViewController(myAlert, animated: true, completion: nil)

上面的代码从数组中收集数据并将其推送到 myAlert 中的操作中。上面的代码在 for 循环中。

在此之后,我使用一个函数来检索最顶层的视图控制器,然后推送下一个警报。

let top = topMostController()
let alertController = UIAlertController(title: "Location pinned", message: "You've successfully pinned this location, good work!", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
    (result : UIAlertAction) -> Void in
    print("OK")
}
alertController.addAction(okAction)

self.presentViewController(myAlert, animated: true, completion: nil)
top.presentViewController(alertController, animated: true, completion: {
     _ in
})

我收到的错误是:

Attempting to load the view of a view controller while it is deallocating and is not allowed and may result in undefined behavior. UIAlertController: 0x1535b1cd0.

有人可以帮我解决这个问题吗?

我想这就是您要找的。第二个必须与第一个的解雇操作一起调用。此外,无论何时使用 UI,使用 dispatch_async(dispatch_get_main_queue()) { \code }

更安全

如果您不确定,您目前在主队列中。

let firstAlertController = UIAlertController(title: "First", message: "This is the first message.", preferredStyle: UIAlertControllerStyle.Alert)

let secondAlertController = UIAlertController(title: "Second", message: "This is the second message.", preferredStyle: UIAlertControllerStyle.Alert)
let secondDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, completion: nil)
secondAlertController.addAction(secondDismissAction)

let firstDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default) {
    UIAlertAction in
    dispatch_async(dispatch_get_main_queue()) {
        self.presentViewController(secondAlertController, animated: true, handler: nil)
    }
}

firstAlertController.addAction(firstDismissAction)
self.presentViewController(firstAlertController, animated: true, completion: nil)