【问题标题】:Show two UIAlertController objects continuously using completion block使用完成块连续显示两个 UIAlertController 对象
【发布时间】:2017-06-22 15:11:31
【问题描述】:

我试图连续显示两个 UIAlertController 的实例,就像下面的代码块。

func showAlerts() {
    let alertA = UIAlertController(title: "Alert A", message: "This is alert a...", preferredStyle: .alert)
    let alertB = UIAlertController(title: "Alert B", message: "This is alert b...", preferredStyle: .alert)

    let alertButton1 = UIAlertAction(title: "OK", style: .default, handler: nil)
    let alertButton2 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertA.addAction(alertButton1)
    alertA.addAction(alertButton2)
    alertB.addAction(alertButton1)
    alertB.addAction(alertButton2)

    self.present(alertA, animated: true) { 
        self.present(alertB, animated: true, completion: { 
            debugPrint("alerts are all shown")
        })
    }
}

我希望这段代码连续显示每个警报,这意味着alertBalertA 之后显示。但是alertB 并没有像我预期的那样出现,控制台上显示警告;

Warning: Attempt to present <UIAlertController: 0x7f7ffde0ace0>  on <ContinuousUIAlertController_Experiment.ViewController: 0x7f7ffdd092d0> which is already presenting <UIAlertController: 0x7f7ffde09f90>

如果我没记错的话,多个 UIAlertController 对象不能同时存在。所以我以某种方式理解上面的警告说明了什么。

那么,如何使用完成UIViewController::present(_:animated:completion:) 或使用几乎相同的逻辑来实现连续警报显示? (我不喜欢使用 UIAlertAction 的处理程序)

如果有解决办法,请告诉我。
我为这个问题苦苦挣扎了几天,但我还没有解决。

【问题讨论】:

  • 同时显示两个警报有什么意义?用户将只能看到一个。为什么在用户关闭第一个后不显示第二个?
  • 您可以将多个自定义视图添加为带有按钮的子视图。
  • @rmaddy 连续显示两个警报,而不是同时显示。是的,我想在第一个消失后显示第二个警报,以某种方式使用完成块。
  • @iAviator 你能详细解释一下吗?如果有一些代码示例,我会毕业!
  • 您的代码不会尝试一遍又一遍地显示警报。它只会尝试同时显示两者,并且只显示一次。

标签: ios swift uialertcontroller


【解决方案1】:

您不能同时显示 2 个 uialettcontrol。

但是你可以在第一个 uialeraction 上显示第二个 uialettcontrol。

例如

let alertController = UIAlertController(title: "iOScreator", message:
            "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
            self.pressed()
        }))

func pressed()
{
    print("you pressed")
}

在按下事件时,您可以为第二个 uialettcontrol 编写代码。

【讨论】:

  • 这应该可以,但是 OP 提到不要使用 UIAlertAction 的处理程序
  • 感谢您的回答。它确实有效,但正如@ronatory 提到的,我不喜欢使用 UIAlertAction 的处理程序,例如完成UIViewController::present(_:animated:completion:)
  • 您还有其他解决方案吗?那么请分享。
猜你喜欢
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多