【问题标题】:Multiple alerts on top of each other in swift多个警报迅速相互叠加
【发布时间】:2018-05-01 08:59:50
【问题描述】:

现在我遇到了错误: 警告:尝试在 CollectionViewController 上呈现 UIAlertController: 0x7f9af9016200: 0x7f9af750d620 已经呈现(null)

有没有办法让我快速堆叠警报? 有我的代码,如果项目在一天前发布,它会显示警报。 我尝试了两种方法但没有成功。

  1. 暂停 for 循环,直到我按下是或否。
  2. 多个警报叠加显示

任何解决方案如何做到这一点?

for p in json! {
     if self.checkDaysPassed(postDate: p["uploadedTime"] as! String) > 1 {
     print("more than one day passed, sell item?")

     let alert = UIAlertController(title: "Sell this item", message: "This item has been unused for a day", preferredStyle: .alert)
     alert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
     alert.addAction(UIAlertAction(title: "Yes", style: .default){ (action) in
           print("pressed yes")
     })
     alert.addAction(UIAlertAction(title: "No", style: .cancel){ (action) in
          print("pressed no")
     })
     self.present(alert, animated: true)
    }
}

【问题讨论】:

  • 而不是多个连续警报呈现一个表格视图,每行都有一个 Sell 按钮和一个全局 Cancel 按钮。
  • 它可能对你有帮助:stackoverflow.com/questions/49732371/…
  • @MJICT 你想只显示一个警报吗?或者对于条件为真但一个接一个警报的所有情况?
  • @Kamran 我想一个接一个地显示警报,每次条件为真时

标签: swift alert uialertcontroller uialertaction


【解决方案1】:

为了一个接一个地显示警报,我建议添加这个递归代码,

messages 添加一个class 变量,并为所有true 条件填充array。之后,您只需调用 showAlert 方法,该方法将处理一一显示所有消息。

class YourClass {

   var messages: [String] = []


   func yourMethod() {
       for p in json! {
         if self.checkDaysPassed(postDate: p["uploadedTime"] as! String) > 1 {
             messages.append("This item has been unused for a day")
         }
       }
       self.showAlert()
   }

   private func showAlert() {
        guard self.messages.count > 0 else { return }

        let message = self.messages.first

        func removeAndShowNextMessage() {
            self.messages.removeFirst()
            self.showAlert()
        }

        let alert = UIAlertController(title: "Sell this item", message: message, preferredStyle: .alert)
        alert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        alert.addAction(UIAlertAction(title: "Yes", style: .default){ (action) in
            print("pressed yes")
            removeAndShowNextMessage()

        })
        alert.addAction(UIAlertAction(title: "No", style: .cancel){ (action) in
            print("pressed no")
            removeAndShowNextMessage()
        })

        UIApplication.shared.delegate?.window??.rootViewController?.present(alert, animated: true)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多