How to pass a variable with a #selector function?And what to pass to textField: UITextField?

Viewed 2276

So I have an UIAlertController, with 2 textfields, I then want to monitor whenever those textfields are edited to see if they meet a certain criteria.

In this case, I want my alert button to be inactive as long as both textfields input is shorter than a number I want to pass to the function.

So I added this target to both textfields:

textField.addTarget(self, action: #selector(self.handleTextFieldEdit), for: .editingChanged)

inside handleTextFieldEdit I have this:

func handleTextFieldEdit(_ textField: UITextField, number: Int)
{
    let number = number
    let alertController:UIAlertController = self.presentedViewController as! UIAlertController;
    let textField: UITextField  = alertController.textFields![0]
    let confirmField: UITextField = alertController.textFields![1]
    let addAction: UIAlertAction = alertController.actions[0];

    if ((textField.text?.characters.count)! > number && (confirmField.text?.characters.count)! > number)
    {
        addAction.isEnabled = true
    }
    else if ((textField.text?.characters.count)! <= number || (confirmField.text?.characters.count)! <= number )
    {
        addAction.isEnabled = false
    }
}

Now my first problem is I can't seem to pass any variables with a #selector func.

My second problem is I don't know what to pass to _ textField: UITextField

I have tried both UITextField and textField (which is what I declared my UITextFields to).

The error that comes up is: Cannot convert value of type 'UITextField'.Type to expected argument type 'UITextField'.

EDIT: All of my code works whenever I don't pass a variable with the function btw. The problem is I want the minimum value to be changed whenever I call the function.

5 Answers
Related