【发布时间】:2016-01-05 07:10:30
【问题描述】:
我已经通过protocols 和delegates 搜索了如何在TableViewCell 中替换UIAlertController 的类似答案。我得到了那个部分的工作。但我在UIAlertController 中有两个动作。其中一个确认一个动作并返回true,另一个按钮是cancel并返回false。
我可以成功展示UIAlertController,但程序只是运行我的代码并忽略了我的确认按钮中的逻辑。我认为这是因为 delegate/protocol 方法在 parallel/asynchronously 中运行delegate 方法并且不等待返回值。
这是我的自定义 TableViewCell 中的代码:
var unattend: Bool!
let customAlert = UIAlertController(title: "Not going anymore?", message: "Event will disappear from this list, confirm?", preferredStyle: UIAlertControllerStyle.Alert)
let nevermindAction = UIAlertAction(title: "Nevermind", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in
unattend = false
}
let unattendAction = UIAlertAction(title: "Confirm unattending", style: UIAlertActionStyle.Destructive) { (action: UIAlertAction) -> Void in
unattend = true
}
customAlert.addAction(nevermindAction)
customAlert.addAction(unattendAction)
self.delegate.goingCancelled(customAlert, unattend: unattend))
这是我的协议:
protocol GoingCancelledDelegate {
func goingCancelled(alert: UIAlertController) -> Bool
}
这是我在 TableViewController 中的代码,是的,我确实在类定义中输入了 GoingCancelledDelegate,只是没有在此处复制和粘贴:
func goingCancelled(alert: UIAlertController) -> Bool {
presentViewController(alert, animated: true) { () -> Void in
}
}
所以警报出现了,但我的UITableViewCell 代码不会等待返回值来处理其他逻辑。任何提示/建议/帮助将不胜感激!提前谢谢!
【问题讨论】:
标签: ios swift2 xcode7 uialertcontroller