This is inside a function, so that any textField passed as argument can respond to the method checkForEmptyFields:
textField.addTarget(self, action: #selector(checkForEmptyFields(sender:)),
for: UIControlEvents.editingChanged)
This is the method checkForEmptyFields:
func checkForEmptyFields(sender:UITextField){
self.loginButton.isEnabled = (sender.text?.trim().isEmpty)! == false
}
This is a simple extension of String for manage the trimming:
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
}
}
Anything seems to work just fine but I'd like to know if this is the right way to proceed and if I forgot something important (or I made any mistake). Thanks!