【发布时间】:2018-01-10 22:14:55
【问题描述】:
我最近一直在互联网上搜索用于 iOS 编程的模态对话框的实现。
我知道 UIAlertViewController。事实上,我目前正在一个应用程序中使用这个实现。但是,我需要一些模态的东西。我需要代码停止执行,直到用户单击警报中的 a 按钮。
到目前为止,我还没有真正看到任何令我满意的实现。这是我的代码的当前状态:
func messageBox(messageTitle: String, messageAlert: String, messageBoxStyle: UIAlertControllerStyle, alertActionStyle: UIAlertActionStyle)
{
var okClicked = false
let alert = UIAlertController(title: messageTitle, message: messageAlert, preferredStyle: messageBoxStyle)
let okAction = UIAlertAction(title: "Ok", style: alertActionStyle)
{
(alert: UIAlertAction!) -> Void in
okClicked = true
}
/* alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: alertActionStyle, handler: { _ in
okClicked = true
NSLog("The \"OK\" alert occured.")
}))*/
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
while(!okClicked)
{
}
}
我应该考虑在情节提要中创建自己的对话框硬核风格还是 swift 有某种我可以使用的实现?
【问题讨论】:
-
为什么需要执行才能停止?为什么你不能在处理程序中为 OK 按钮做需要的事情?
-
使用 while() 听起来不太好。必要时使用完成块。
-
你在任何情况下都不应该做这样的事情。虽然您的应用程序在显示警报时可能什么都不做,但它仍应具有响应能力。您尝试这样做的原因是什么?
标签: ios swift dialog modal-dialog