防止解除 UIAlertController [英] Prevent dismissal of UIAlertController

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

问题描述

我将一个 UITextField 添加到一个 UIAlertController,它显示为一个 AlertView.在关闭 UIAlertController 之前,我想验证 UITextField 的输入.根据验证,我想关闭 UIAlertController 或不关闭.但我不知道如何防止按下按钮时 UIAlertController 的解除操作.有没有人解决了这个问题或任何想法从哪里开始?我去了谷歌,但没有运气:/谢谢!

I am adding a UITextField to a UIAlertController, which appears as an AlertView. Before dismissing the UIAlertController, I want to validate the input of the UITextField. Based on the validation I want to dismiss the UIAlertController or not. But I have no clue how to prevent the dismissing action of the UIAlertController when a button is pressed. Has anyone solved this problem or any ideas where to start ? I went to google but no luck :/ Thanks!

推荐答案

你说得对:如果用户可以点击警报中的按钮,警报将被解除.所以你想阻止用户点击按钮!这只是禁用 UIAlertAction 按钮的问题.如果警报操作被禁用,用户将无法点击它来关闭.

You're correct: if the user can tap a button in your alert, the alert will be dismissed. So you want to prevent the user from tapping the button! It's all just a matter of disabling your UIAlertAction buttons. If an alert action is disabled, the user can't tap it to dismiss.

要将其与文本字段验证结合使用,请使用文本字段委托方法或操作方法(在创建文本字段时在文本字段的配置处理程序中进行配置)以根据文本具有(或未具有)适当地启用/禁用 UIAlertActions) 已输入.

To combine this with text field validation, use a text field delegate method or action method (configured in the text field's configuration handler when you create it) to enable/disable the UIAlertActions appropriately depending on what text has (or hasn't) been entered.

这是一个例子.我们像这样创建了文本字段:

Here's an example. We created the text field like this:

alert.addTextFieldWithConfigurationHandler {
    (tf:UITextField!) in
    tf.addTarget(self, action: "textChanged:", forControlEvents: .EditingChanged)
}

我们有一个取消操作和一个 OK 操作,我们将 OK 操作带入了禁用状态:

We have a Cancel action and an OK action, and we brought the OK action into the world disabled:

(alert.actions[1] as UIAlertAction).enabled = false

随后,除非文本字段中有一些实际文本,否则用户无法点击确定":

Subsequently, the user can't tap OK unless there is some actual text in the text field:

func textChanged(sender:AnyObject) {
    let tf = sender as UITextField
    var resp : UIResponder = tf
    while !(resp is UIAlertController) { resp = resp.nextResponder() }
    let alert = resp as UIAlertController
    (alert.actions[1] as UIAlertAction).enabled = (tf.text != "")
}

编辑这是上述代码的当前(Swift 3.0.1 及更高版本)版本:

EDIT Here's the current (Swift 3.0.1 and later) version of the above code:

alert.addTextField { tf in
    tf.addTarget(self, action: #selector(self.textChanged), for: .editingChanged)
}

alert.actions[1].isEnabled = false

@objc func textChanged(_ sender: Any) {
    let tf = sender as! UITextField
    var resp : UIResponder! = tf
    while !(resp is UIAlertController) { resp = resp.next }
    let alert = resp as! UIAlertController
    alert.actions[1].isEnabled = (tf.text != "")
}

这篇关于防止解除 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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