How to disable UIAlertAction depending on UITextFi

2019-05-09 03:43发布

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 :

enter image description here

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 ?

2条回答
We Are One
2楼-- · 2019-05-09 04:21

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-09 04:24

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 ...

查看更多
登录 后发表回答