swift 代码 : Is there a way to make the alert view disappear automatically

标签 swift

有没有办法让警报 View 在几秒钟后自动消失,无需用户操作。目前我的代码如下,它要求用户按确定以消失警报对话框。我想显示警报而不需要用户干预,只需让警报在几秒钟内消失。感谢您的任何评论。

我的代码如下:

func showAlertController (message: String) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(alertController, animated: true, completion: nil)
}

最佳答案

您可以使用dispatch_after延迟任何事情。例如,这将在 3 秒后关闭警报 View 。

let delayTime = dispatch_time(DISPATCH_TIME_NOW, 
                              Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    presentedViewController.dismissViewControllerAnimated(true, completion: nil);
}

您还可以使用@matt's awesome delay function .

关于 swift 代码 : Is there a way to make the alert view disappear automatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362063/

相关文章:

ios - 在 Swift 中按顺序播放音频文件,其中第二个声音重复播放

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 我怎样才能让对象(我通过对象库添加的)分层在 Xcode 中以编程方式创建的对象之上?

ios - 如何在 user?.getDetails 调用后从 AWS Cognito 用户池获取特定属性

swift - 在 ios 8/swift 中使用委托(delegate)的 AppLovin 集成

ios - 在 SceneKit 中将形状添加到球体表面

swift - 应用自动布局后 Collectionview 崩溃

ios - 如何在 Swift 泛型类中使用 AnyClass

swift - 参数中的运算符(Swift)

ios - 通过 Firebase 发送通知