【问题标题】:Trying to present 2 UIAlertControllers back to back试图背靠背呈现 2 个 UIAlertController
【发布时间】:2016-06-29 17:51:47
【问题描述】:

我有一个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
})

我收到的错误是:

尝试加载视图控制器的视图 释放并且是不允许的,并且可能导致未定义的行为。 UIAlertController: 0x1535b1cd0.

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

【问题讨论】:

  • 为什么在第二个代码块中调用self.presentViewController(myAlert, animated: true, completion: nil)?您不是已经在第一个代码块中显示了第一个警报吗?另外,您可以使用第一个警报的完成块来显示第二个(有条件地基于某些用户操作)吗?
  • 您可以在原始 AlertController 的操作处理程序中调用确认 AlertController 的呈现。无需在最顶层的控制器上运行它。只需在自己上运行它,就像你对原始版本所做的那样。此外,您的操作闭包中有一个保留周期。您需要将 self 捕获为 [unowned self] 或 [weak self] 以防止闭包中的隐式强保留。
  • 好吧,我为 myAlert 移动了 self.presentViewController 但这并不影响它。 SARnab,你能详细说明一下吗?

标签: ios swift uiviewcontroller alert uialertcontroller


【解决方案1】:

我认为这就是您要寻找的。第二个必须与第一个的解雇动作一起被调用。此外,无论何时使用 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)

【讨论】:

  • Sethmr,我使用 dispatch_async 来填充 myAlert 的警报操作,因为我正在从数组中推送这些操作。但是,我对两个警报控制器都使用 dispatch_async 的意思不明白
  • 我刚刚测试了自己写的函数。如果您只是将我的文本替换为您的文本,它将运行。您只能将 dispatch_async 用于presentingViewController 行,并且仅用于您可能不在主队列中的那些。出于我的目的,我将我的函数放在 viewDidLoad 中,它在主队列上运行,除非你调用带有完成处理程序闭包的异步函数,所以我只将它放在 firstDismissAction 中。
  • 第二个 alertController 必须像在第一个警报控制器的解除操作中一样被调用。
猜你喜欢
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多