一段时间后如何显示UIAlertView?

时间:2016-11-02 09:38:33

标签: ios swift xcode8 ios10 swift2.3

我试图在一段时间后显示UIAlertView(比如在应用程序中执行某些操作后的5分钟)。我已经在应用程序关闭或在后台通知用户。但我希望在应用程序运行时显示UIAlertView。我不想一次又一次地刷新。三分钟后,我必须显示一些警告,比如你想保存或不保存?

4 个答案:

答案 0 :(得分:1)

使用

self.perform(#selector(displayAlert), with: nil, afterDelay: 300)

其中display alert是显示警报的功能。

答案 1 :(得分:0)

我认为对于具有自定义延迟的UIAlertView,有一种替代的最佳方法是您可以使用timeInterval安排本地通知,无论您想要什么。

答案 2 :(得分:0)

最后,我找到了一个问题的答案: 我使用以下代码的轻击手势:

func displayAlert()
    {
        let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertControllerStyle.alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
        self.timer = NSTimer.scheduledTimerWithTimeInterval(50.0, target: self, selector: #selector(SONGS.displayAlert), userInfo: nil, repeats: false)

        timer.invalidate()// stop the timer
}

如果设置重复:true,如果你想停止计时器,它将每隔50秒显示一次警报,你可以在任何你想要的地方使用timer.invalidate()。 三江源

答案 3 :(得分:0)

这对我有用:

self.timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(SONGS.displayAlert), userInfo: nil, repeats: false)
func displayAlert()
{
   self.message.dismissWithClickedButtonIndex(0,animated: true)
}
相关问题