【问题标题】:I need to do some check in the UIAlertAction and avoid dismiss of the UIAlertController我需要检查 UIAlertAction 并避免关闭 UIAlertController
【发布时间】:2019-09-10 12:58:51
【问题描述】:

我有一个UIAlertController、一个TextField 和两个UIAlertAction。 我需要检查UIAlertAction 之一中TextField 的字符串,如果TextField 中的字符串不符合要求,请防止UIAlertController 被解雇

let saveAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { alert -> Void in
    let titleTextField = alertController.textFields![0] as UITextField

    //Prevent dismiss if text in textField doesn't match requirements

})

我已经尝试过类似的方法:

(alertController.actions[1] as UIAlertAction).isEnabled = false

但是在UIAlertAction这个已经太晚了

那么有什么方法可以防止在 UIAlertAction 的句柄中被解雇?

【问题讨论】:

  • 我不这么认为,我需要检查按钮的点击情况,而不仅仅是禁用按钮以避免该操作
  • 也许 UIAlertController 不适合此任务。
  • “不可能”的回答也是可以接受的,但我想确定我没有错过什么
  • @MathieuRobert 目前不可能。点击按钮后,警报将被解除。解决这个问题的唯一方法是通过 UITextFieldDelegate 方法并基于此启用/禁用按钮。
  • @PGDev 好的,谢谢,知道我确定我需要以另一种方式做我想做的事^^

标签: swift uialertcontroller swift5 uialertaction


【解决方案1】:

您可以设置 UITextField 委托,而不是尝试在 UIAlertAction 中停止关闭,

func addTextField(configurationHandler: ((UITextField) -> Void)? = nil)

方法和使用委托调用,

optional func textFieldDidEndEditing(_ textField: UITextField)

启用和禁用 UIAlertAction。

【讨论】:

  • 我需要让按钮至少可点击一次,以便根据用户在 TextField 中输入的内容向用户显示消息
猜你喜欢
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多