Ios 正在尝试背靠背演示2个UIAlertController

Ios 正在尝试背靠背演示2个UIAlertController,ios,swift,uiviewcontroller,alert,uialertcontroller,Ios,Swift,Uiviewcontroller,Alert,Uialertcontroller,我有一个UIAlertController,其中的选项是从数组填充的,并呈现给用户。然后,用户从警报中选择一个选项。在这之后,我有一个单独的警报,向用户提供一条确认消息,其中有一个OK按钮 myAlert.addAction(UIAlertAction.init(title: item, style: .Default, handler: { (UIAlertAction) in self.chosenBusiness.append(businessNameData[

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

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
})
我收到的错误是:

正在尝试在视图控制器处于运行状态时加载视图控制器的视图 不允许取消分配和,并可能导致未定义的行为。 UIAlertController:0x1535b1cd0


有人能帮我吗

我想这就是你要找的。第二个必须与第一个的解雇行动一起进行。此外,无论何时使用UI,使用
dispatch\u async(dispatch\u get\u main\u queue())都更安全{
\\代码}

如果您不确定,则表示您当前在主队列中

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)

我想这就是你要找的。第二个必须与第一个的解雇行动一起进行。此外,无论何时使用UI,使用
dispatch\u async(dispatch\u get\u main\u queue())都更安全{
\\代码}

如果您不确定,则表示您当前在主队列中

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)

为什么在第二段代码中调用self.presentViewController(myAlert,动画:true,完成:nil)?您是否已经在第一个代码块中显示了第一个警报。此外,您是否可以使用第一个警报的完成块来显示第二个警报(有条件地基于某些用户操作)?您可以在原始AlertController的操作处理程序中调用确认AlertController的表示。不需要在最顶层的控制器上运行它。只需在自己身上运行它,就像您在原版中所做的那样。此外,您的操作结束还有一个保留周期。您需要将self捕获为[无主self]或[弱self],以防止闭包中隐含的强保留。我为myAlert移动了self.presentViewController,但这并不影响它。SArnab,你能详细说明一下lil吗?为什么在第二段代码中调用
self.presentViewController(myAlert,动画:true,完成:nil)
?您是否已经在第一个代码块中显示了第一个警报。此外,您是否可以使用第一个警报的完成块来显示第二个警报(有条件地基于某些用户操作)?您可以在原始AlertController的操作处理程序中调用确认AlertController的表示。不需要在最顶层的控制器上运行它。只需在自己身上运行它,就像您在原版中所做的那样。此外,您的操作结束还有一个保留周期。您需要将self捕获为[无主self]或[弱self],以防止闭包中隐含的强保留。我为myAlert移动了self.presentViewController,但这并不影响它。SArnab,你能详细说明一个lil more吗?Sethmr,我已经使用dispatch_async来填充myAlert的警报动作,因为我正在从数组中推送这些动作。但是,我不明白您对两个AlertControllers使用dispatch_async是什么意思,我刚刚测试了我编写的函数。如果你只是用你的文本替换我的文本,它就会运行。您将仅对presentingViewController行使用dispatch_async,并且仅对可能不在主队列中的行使用dispatch_async。出于我的目的,我将函数放在viewDidLoad中,该函数在主队列上运行,除非您使用完成处理程序闭包调用异步函数,因此我只将其放在firstDismissAction中。第二个alertController必须像第一个alert controller的Dismission操作一样调用。Sethmr,我使用dispatch_async来填充myAlert的警报操作,因为我正在从数组中推送这些操作。但是,我不明白您对两个AlertControllers使用dispatch_async是什么意思,我刚刚测试了我编写的函数。如果你只是用你的文本替换我的文本,它就会运行。您将仅对presentingViewController行使用dispatch_async,并且仅对可能不在主队列中的行使用dispatch_async。出于我的目的,我将函数放在viewDidLoad中,该函数在主队列上运行,除非您使用完成处理程序闭包调用异步函数,因此我只将其放在firstDismissAction中。第二个alertController必须像第一个alert控制器的Dismission操作一样调用。