How to disable UIAlertAction depending on UITextField in UIAlertController?

末鹿安然 提交于 2020-01-02 04:45:08

问题


I'm using a UIAlertController to present the user with a dialog to enter a 5 digit CRN. I want the Add button to be disabled until there are 5 and only 5 digit in the UITextField.

Here's what the UI looks like :

Here's the properties setup for the UIAlertController :

var alertController: UIAlertController!

var addAlertAction: UIAlertAction! {
    return UIAlertAction(title: "Add", style: .default)
}

Here's how I'm initializing them in the viewDidLoad method :

self.alertController = UIAlertController(title: "Add Class", message: "Input CRN", preferredStyle: .alert)
self.alertController.addAction(addAlertAction)
self.alertController.addAction(UIAlertAction(title: "Cancel", style: .destructive))
self.alertController.addTextField { (textField) in
    textField.delegate = self
    textField.keyboardType = .numberPad
}

Here's how I'm trying to disable/enable the button using the UITextFieldDelegate method :

func textFieldDidEndEditing(_ textField: UITextField) {
    if ((textField.text?.characters.count)! == 5) {
        self.alertController.actions[0].isEnabled = true
    } else {
        self.alertController.actions[0].isEnabled = false
    }
}

However, the button remains disabled (or enabled) all the time. It never gets enabled. What's going wrong ?


回答1:


Implement the logic in the shouldChangeCharactersIn delegate method of UITextField. This method gets fired on change in each character of textfield. You can build the logic taking the range parameter into consideration.

Here is the code that works perfectly.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if ((range.location == 4 && range.length == 0) || (range.location == 5 && range.length == 1)) {
        self.alertController.actions[0].isEnabled = true
    }else{
        self.alertController.actions[0].isEnabled = false
    }

    return true;
}

Tested successfully in Swift 3, XCode 8 and iOS 10

Hope that helps. Happy coding ...




回答2:


Try using the textfield's EditingChanged event as follow:

let addAction:UIAlertAction = UIAlertAction(title: "Add", style: .default)
addAction.isEnabled = false; //to make it disable while presenting

self.alertController = UIAlertController(title: "Add Class", message: "Input CRN", preferredStyle: .alert)
self.alertController.addAction(addAction)
self.alertController.addAction(UIAlertAction(title: "Cancel", style: .destructive))
self.alertController.addTextField { (textField) in
    textField.keyboardType = .numberPad
    textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(field:)), for: UIControlEvents.editingChanged)
}

And the listener for text field should look like this:

func alertTextFieldDidChange(field: UITextField){
    let alertController:UIAlertController = self.presentedViewController as! UIAlertController;
    let textField :UITextField  = alertController.textFields![0];
    let addAction: UIAlertAction = alertController.actions[0];
    addAction.isEnabled = (textField.text?.characters.count)! >= 5;

}

Tested with Xcode 8 and swift 3. Worked for me.



来源:https://stackoverflow.com/questions/39538098/how-to-disable-uialertaction-depending-on-uitextfield-in-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!