【发布时间】: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