Swift中的UIAlert会自动消失吗?

时间:2016-01-29 21:28:40

标签: ios swift uialertcontroller

我有以下代码:

    /// Creates Alerts on screen for user.
func notifyUser(title: String, message: String) -> Void
{
    let alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)
}

其中显示以下警告:

enter image description here

我希望警报可能会显示1-2秒,并且无需单击“确定”或“关闭”即可自动关闭。这可能吗?

6 个答案:

答案 0 :(得分:4)

是的,这完全有可能,我认为@Duncan C方法会很好用,并且它是自我解释的,所以我将用代码解释你的@Duncan方法,另一种方法是使用 Grand Central Dispatch的延迟(GCD)。

  

第一种方法:使用NSTimer

// set the UIAlerController property
var alert: UIAlertController!

func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
    alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)

    // setting the NSTimer to close the alert after timeToDissapear seconds.
    _ = NSTimer.scheduledTimerWithTimeInterval(Double(timeToDissapear), target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
}
  

第二种方法:使用GCD

// set the UIAlerController property
var alert: UIAlertController! 

func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
    alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)

    // Delay the dismissal by timeToDissapear seconds
    let delay = Double(timeToDissapear) * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) { [weak self] in
        self!.alert.dismissViewControllerAnimated(true, completion: nil)
    }
}

然后你可以通过以下方式在任何你想要的地方调用它:

self.notifyUser("Hello", message: "World", timeToDissapear: 3)

我希望这对你有所帮助。

答案 1 :(得分:1)

不确定。 UIAlertControllerUIViewController的特殊类型。您正在使用presentViewController:animated:completion:显示它。只需将指向UIAlertController的指针保存到实例变量中,启动计时器,当计时器触发时,调用dismissViewControllerAnimated:completion:。在这种情况下,您可能希望摆脱“确定”按钮操作,如果您离开“确定”按钮,则在计时器触发之前单击“确定”时,您需要测试并确保代码正常工作。

答案 2 :(得分:1)

这是Swift 4的代码,请参考...谢谢

  let alert = UIAlertController(title: "Success", message: "Record Updated Successfully", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
        switch action.style{
        case .default:
            print("default")

        case .cancel:
            print("cancel")

        case .destructive:
            print("destructive")

        }}))
    self.present(alert, animated: true, completion: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {

        alert.dismiss(animated: true, completion: nil)
    }

答案 3 :(得分:0)

试试这段代码:

var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "dismissAlert", userInfo: nil, repeats: true)


func dismissAlert()
{
     // Dismiss the alert from here
     alert.dismissViewControllerAnimated(false, completion: nil)

}

我试过了,这很好用。您可以在

中设置计时器
  

scheduledTimerWithTimeInterval

其中当前时间设置为5.0秒

答案 4 :(得分:0)

func alert2 (_ dictKey: String){
    if self.presentedViewController == nil {
        let alertController = UIAlertController(title: nil,     message: dictKey, preferredStyle: .alert )
        alertController.addAction(UIAlertAction(title: "START AGAIN", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in self.returnToStart()}))
        alertController.addAction(UIAlertAction(title: "REQUEST PIN", style: UIAlertActionStyle.default, handler:{(action:UIAlertAction!) in self.pinCreate()
        self.dismiss(animated: false, completion: nil)//This dismisses the alert
        }))
        self.present(alertController, animated: true,completion: nil)
    }

}

这被证明是一个简单的解决方案

答案 5 :(得分:0)

 func notifyUser(message: String) -> Void {
  let alert = UIAlertController(title: "", message: message, preferredStyle: UIAlertController.Style.alert)
  present(alert, animated: true, completion: nil)
  DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [unowned self] in
   self.dismiss(animated: true)
  }
 }

要使用:

  notifyUser(message: "Image Saved")