如何在 For 循环中使用 UIAlertController [英] How to use UIAlertController in For loop

查看:38
本文介绍了如何在 For 循环中使用 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个可能需要重命名的场景,我想使用 UIAlertController 来收集新名称.我希望它能够同时进行多项更改(虽然不太可能,但我想支持它).

I've got a scenario where something may need to be renamed and I'd like to use UIAlertController to collect the new name. I'd like this to be robust for several changes at once (although it's unlikely, I'd like to support it).

问题是,只有当当前 UIAlertController 的结果出现时,我才需要循环继续,事实上,循环立即继续,当然不能在一次.我一整天都被困在这个问题上,需要帮助.

The issue is that I need the loop to continue only when the results of the current UIAlertController are in, as it is, the loop continues immediately and of course can't present more than one at once. I've been stuck on this all day and need help.

我需要某种使用完成处理程序为下一次循环迭代开绿灯的方法.

I need some kind of method that uses the completion hander to give the green light to the next loop iteration.

这是问题的简化版本,changeNames 函数位于 UIViewController 中.

This is a simplified version of the problem, the changeNames func is in a UIViewController.

var names = ["Bob","Kate"]

func changeNames(){
    for name in names {
        let alert = UIAlertController(title: "Rename \(name)",
                                      message: "What would you like to call \(name)?",
                                      preferredStyle: .alert)
        alert.addTextField(configurationHandler: { (textField) in
            textField.text = name
        })
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
            print("User changed \(name) to \(alert!.textFields![0].text)")
            //Don't continue the loop until now
        }))
        self.present(alert, animated: true)
    }
}

谢谢

推荐答案

循环很难用异步代码,我宁愿用递归:

Looping is hard with asynchronous code, I'd rather go with recursion:

func showChangeAlerts(for texts: [String]) {
  guard let value = texts.first else { return }

  let alert = UIAlertController(title: "Rename \(value)",
    message: "What would you like to call \(value)?",
    preferredStyle: .alert)
  alert.addTextField(configurationHandler: { (textField
    textField.text = value
  })
  alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert, weak self] (_) in
    let newValue = alert?.textFields![0].text ?? ""
    print("User changed \(value) to \(newValue)")
    self?.showChangeAlerts(for: Array(texts.dropFirst()))
  }))
  self.present(alert, animated: true)
}

您传入一个字符串数组,此方法将遍历它们,直到处理完所有字符串.

You pass in an array of strings, and this method will work through them until all have been processed.

编辑

好的,刚刚看到 Ron Nabuurs 用一个非常相似的解决方案击败了我 :) 主要区别在于我的方法不使用额外的迭代器参数,而是消耗"提供的数组中的字符串,直到它为空.但是原理是一样的.

Ok, just saw that Ron Nabuurs beat me with a very similar solution :) The main difference is that my method doesn't use the additional iterator parameter, but "consumes" strings from the provided array until it's empty. But the principle is the same.

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

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