Yes, you can use Outlet to make this check.
Basically you would do that:
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
func textFieldDidBeginEditing(textField: UITextField) {
if textField == text1 {
print("Text 1 mudou");
} else if textField == text2 {
print("Text 2 mudou");
} else {
print("outro text mudou");
}
}
Another way you can do this is by checking the Uitextview tag. For example, taking into account that text1 has the tag '1' and text2 with the tag '2' the code would look like this:
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
func textFieldDidBeginEditing(textField: UITextField) {
if(textField.tag == 1) {
print("Text 1 changed");
} else if(textField.tag == 2) {
print("Text 2 changed");
} else {
print("Text ?? changed");
}
}
Taking into account both forms, it is possible to observe that through the Outlet the code becomes more readable.
Edit: As a complement to Luis, it is also possible to use constants to compare tags, as in the example:
let kTextField1 = 1;
let kTextField2 = 2;
func textFieldDidBeginEditing(textField: UITextField) {
if(textField.tag == kTextField1) {
print("Text 1 changed");
} else if(textField.tag == kTextField2) {
print("Text 2 changed");
} else {
print("Text ?? changed");
}
}
I do not know if there is a more efficient way than to create these conditions from the
IBOutlet
for eachUITextField
. Exactly as you described.– Paulo Rodrigues
@Paulorodrigues, that’s right, I’ll do it that way.
– Arubu