Swift 中的 UIAlert 会自动消失? [英] UIAlert in Swift that automatically disappears?

查看:29
本文介绍了Swift 中的 UIAlert 会自动消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

///在屏幕上为用户创建警报.func notifyUser(title: String, message: String) ->空白{让 alert = UIAlertController(title: title,消息:消息,优选样式:UIAlertControllerStyle.Alert)let cancelAction = UIAlertAction(title: "OK",样式:.Cancel,处理程序:nil)alert.addAction(cancelAction)UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animation: true,完成:无)}

显示以下警报:

我希望警报出现大约 1-2 秒并自动关闭,而无需单击确定"或关闭".这可能吗?

解决方案

是的,这是完全可能的,我认为 @Duncan C 方法会工作得很好,而且不言自明,所以我将在代码中向您解释 @Duncan 方法另一种方法是通过 Grand Central Dispatch (GCD) 使用延迟.

<块引用>

第一种方法:使用 NSTimer

//设置 UIAlerController 属性var alert: UIAlertController!func notifyUser(title: String, message: String, timeToDissapear: Int) ->空白{警报 = UIAlertController(标题:标题,消息:消息,优选样式:UIAlertControllerStyle.Alert)let cancelAction = UIAlertAction(title: "OK",样式:.Cancel,处理程序:nil)alert.addAction(cancelAction)UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animation: true,完成:无)//设置 NSTimer 以在 timeToDissapear 秒后关闭警报._ = NSTimer.scheduledTimerWithTimeInterval(Double(timeToDissapear), target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)}

<块引用>

第二种方法:使用 GCD

//设置 UIAlerController 属性var alert: UIAlertController!func notifyUser(title: String, message: String, timeToDissapear: Int) ->空白{警报 = UIAlertController(标题:标题,消息:消息,优选样式:UIAlertControllerStyle.Alert)let cancelAction = UIAlertAction(title: "OK",样式:.Cancel,处理程序:nil)alert.addAction(cancelAction)UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animation: true,完成:无)//延迟解除 timeToDissapear 秒让延迟 = Double(timeToDissapear) * Double(NSEC_PER_SEC)让时间 = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))dispatch_after(time, dispatch_get_main_queue()) { [weak self] inself!.alert.dismissViewControllerAnimated(true, completion: nil)}}

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

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

希望对您有所帮助.

I have the following code:

    /// 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)
}

Which shows the following Alert:

I would prefer the Alert to appear for maybe 1-2 seconds and auto dismiss without having to click ok or dismiss. Is this possible?

解决方案

Yes it's completely possible, I think the @Duncan C approach will work very well and it's self explanatory, so I going to explain you in code the @Duncan approach and another approach is using delays with the Grand Central Dispatch(GCD).

First Approach: Using the NSTimer class

// 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)
}

Second Approach: Using 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)
    }
}

And then you can call it in anywhere you want like in the following way :

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

I hope this help you.

这篇关于Swift 中的 UIAlert 会自动消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆