ios - UIAlertView 没有在正确的时间显示

标签 ios iphone swift

我在简单 View 中有一个带有点击功能的按钮:

@IBAction func watchAlert(sender: AnyObject) {
    showAlert()
    for i in 1...2{
    AudioServicesPlaySystemSound (1005);
    sleep(2)
    }

}

func showAlert(){
    var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
    self.presentViewController(alert, animated: true, completion: nil)
    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: dismissAlert ))

}


func dismissAlert(alertView: UIAlertAction!)
{
    println("User click ok button")
}

即使我试图在播放音频之前调用 showAlert(),我也只能在播放声音后才能看到警报显示。我希望首先出现警报,然后播放声音。 任何帮助表示赞赏。

最佳答案

取出 sleep 命令(你必须 never sleep 主线程 - 看门狗进程会杀了你,此外,你错误地卡住了整个界面)。相反:显示警报,然后使用延迟性能(如我的 delay 实用程序 here )来播放声音。

(您的代码还有其他问题;特别是,您还需要找到一种新的方法来播放两次声音,因为您不能使用 sleep;这也可以使用 delay - 嵌套完成。)

关于ios - UIAlertView 没有在正确的时间显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29434660/

相关文章:

iphone - 便捷方法与 init 方法的内存使用情况

arrays - 将 Firebase 数据附加到 Swift ios 中的数组

swift - 在前台、后台和杀死(从后台删除)应用程序中检测信标

ios - ApplicationDelegate 单例 iphone

iphone - NSDate未正确转换

iphone - iOS : can I use one timer to trigger multiple things at certain times?

Swift 3/4 - 从椭圆曲线 key 对生成 SecCertificate

ios - 通过游戏中心应用程序进入游戏时如何接收基于回合的匹配数据?

ios - 第二次单击自定义选项卡栏时滚动到 View 顶部 : Objective-C

ios - 您如何快速访问用户联系人的电话号码?