【问题标题】:How do I present multiple alert controllers in a for loop in swift如何在 swift 的 for 循环中显示多个警报控制器
【发布时间】:2023-03-04 11:23:01
【问题描述】:

我在一个接一个地显示多个警报控制器时遇到问题。我想做的是让我的 for 循环等到用户关闭响应控制器。 我的显示消息代码如下。

for block in blocks {
     self.presentMessage(title: codeBlock.stringInput1, message: codeBlock.stringInput2)
}
func presentMessage(title: String, message: String, completion: @escaping (Bool)->()) {
     let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

     alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
          completion(true)
     }))

     self.present(alert, animated: true)
}

编辑 --

这是我的块类

class block: Codable {
    var type: String
    var stringInput1: String
    var stringInput2: String

    init(t: String, i1: String, i2: String) {
        type = t
        stringInput1 = i1
        stringInput2 = i2
    }
}

我已经尝试使用调度组,但未能使其正常工作。我设置的最终目标是让块能够根据块的类型执行不同的操作。 如果可能的话,我想让我的 for 循环等待函数完成,直到它继续。

【问题讨论】:

    标签: ios swift xcode uikit


    【解决方案1】:

    Swift 只允许您一次呈现一个视图控制器,因此您必须执行以下操作:

    var blocks: [Block] = [Block.init(one: "1", two: "12"),
                            Block.init(one: "2", two: "22"),
                            Block.init(one: "3", two: "32")]
    
    func nextMessage(index: Int) {
        if index < blocks.count {
            let codeBlock = blocks[index]
            self.presentMessage(index: index, title: codeBlock.stringInput1, message: codeBlock.stringInput2)
        }
    }
    func presentMessage(index: Int, title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
        alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
            self.nextMessage(index: index + 1)
        }))
    
         self.present(alert, animated: true)
    }
    

    从数组中的第一项调用 presentMessage 将启动它:

    self.presentMessage(index: 0, title: blocks[0].stringInput1, message: blocks[0].stringInput2)
    

    这是我创建的快速而肮脏的 Block 类,但发布任何 将来像这样的相关代码将对那些人有所帮助 回答问题。

    class Block {
        var stringInput1: String
        var stringInput2: String
    
        init(one: String, two: String) {
            stringInput1 = one
            stringInput2 = two
        }
    }
    

    【讨论】:

    • 感谢您的帮助。目前这是我的块设置swift class block: Codable { var type: String var stringInput1: String var stringInput2: String init(t: String, i1: String, i2: String) { type = t stringInput1 = i1 stringInput2 = i2 } } 它现在设置为扩展为不同类型的块,然后只是消息。是否可以让我的 for 循环等到函数完成?
    • 您可以使用自定义的 DispatchGroups 或 Operations,但这些更适用于长时间运行的后台任务。它们实施起来也有点复杂,不正确的使用可能会导致您的应用挂起。
    • 如果它解决了您的问题,请接受此答案。很高兴我能帮上忙! :-)
    【解决方案2】:

    好的,经过一些工作,我将@elliott-io 的答案改编成对我有用的东西。这是我的问题的最终解决方案。

    func nextBlock(index: Int) {
        if index < codeBlocks.count {
            let type = codeBlocks[index].type
            if type == "message" {
                self.presentMessage(title: codeBlocks[index].stringInput1, message: codeBlocks[index].stringInput2, index: index)
            } else if type == "link" {
                self.openLink(webpage: codeBlocks[index].stringInput1, index: index)
            }
        }
    }
    
    func presentMessage(title: String, message: String, index: Int) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
        alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
            self.nextBlock(index: index + 1)
        }))
    
        self.present(alert, animated: true)
    }
    

    我使用它作为我的解决方案的原因是我希望能够拥有一个块可以做的多种事情。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2011-09-21
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多