循环多个 UIAlertController 的

编程入门 行业动态 更新时间:2024-10-12 05:50:54
本文介绍了循环多个 UIAlertController 的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在某些情况下,我的应用程序需要显示多个警报消息.错误消息是在启动时收集的,需要一次向用户显示一个.当第一个被确认时,应该呈现下一个.问题是,很明显,它们都试图同时执行.有没有一种聪明的方法可以同步执行此操作?下面是一些简单描述我想要做什么的代码:

In some cases my applications needs to display muliple Alert messages. Error messages are gathered on start and needs to be displayed to the user one at a time. When the first one is acknowledged, the next one should be presented. The problem is that they all try to execute at the same time, obviously. Is there a smart way to do this synchronously? Here is some code that simply describes what I want to do:

var errors : [NSError]!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let error1 = NSError(domain: "Test1", code: 1, userInfo: [NSLocalizedFailureReasonErrorKey : "Test1 reason."])

    let error2 = NSError(domain: "Test2", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test2 reason."])

    let error3 = NSError(domain: "Test3", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test3 reason."])

    errors = [error1, error2, error3]

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    for error in errors {

        self.showAlert(error)

    }

}

func showAlert(error: NSError) {

    var alert = UIAlertController(title: error.domain, message: error.localizedDescription, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:nil))

    self.presentViewController(alert, animated: true, completion: nil)
}

推荐答案

您就快到了.拥有一个警报消息缓冲区是正确的想法.但不是立即显示所有警报,您应该将 showAlert() 调用移动到 UIAlertAction 的处理程序.因此,如果解除了一个警报,则会显示下一个警报.

You are almost there. Having a buffer of alert messages is the correct idea. But instead of showing all the alerts immediately, you should move the showAlert() call to the handler of the UIAlertAction. So if one alert is dismissed, the next will be shown.

像这样:

var errors : [NSError]!

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let error1 = NSError(domain: "Test1", code: 1, userInfo: [NSLocalizedFailureReasonErrorKey : "Test1 reason."])
    let error2 = NSError(domain: "Test2", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test2 reason."])
    let error3 = NSError(domain: "Test3", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test3 reason."])

    errors = [error1, error2, error3]

    showError() // show an alert if errors are queued
}

func showError() {
    if let error = errors.first {
        let alert = UIAlertController(title: error.domain, message: error.localizedDescription, preferredStyle: .Alert)
        let okayAction = UIAlertAction(title: "OK", style: .Default) { action in
            self.errors.removeAtIndex(0) // remove the message of the alert we have just dismissed

            self.showError() // show next alert if there are more errors queued
        }
        alert.addAction(okayAction)
        presentViewController(alert, animated: true, completion: nil)
    }
    else {
        println("All alerts shown")
    }
}

<小时>

忠告:关闭多个警报非常烦人.也许您可以创建一个专用的全屏 viewController 来显示 UITableView 或其他内容中的所有错误消息.这当然取决于典型用户将看到的警报消息的数量.如果它经常超过三个,我会使用一个模式 UIViewController,它可以一目了然地显示所有消息.


Word of advice: Dismissing multiple alerts is very annoying. Maybe you could create a dedicated full screen viewController that shows all the error messages in a UITableView or something. This of course depends on the number of alert messages a typical user will see. If it's regularly more than three I would use a modal UIViewController which shows all messages at a glance.

这篇关于循环多个 UIAlertController 的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 14:14:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   UIAlertController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!