Dismiss UIAlertView after 5 Seconds Swift

后端 未结 10 1756
悲哀的现实
悲哀的现实 2020-12-22 20:31

I\'ve created a UIAlertView that contains a UIActivityIndicator. Everything works great, but I\'d also like the UIAlertView to disappear after 5 seconds.

Ho

10条回答
  •  鱼传尺愫
    2020-12-22 20:57

    Create the alert object as a global variable. You can use a NSTimer for this purpose.

    var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
    
    
    func dismissAlert()
    {
        // Dismiss the alert from here
        alertView.dismissWithClickedButtonIndex(0, animated: true)
    
    }
    

    NOTE:

    Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.

    Reference : UIAlertView

提交回复
热议问题