If you don't want to add an IBOutlet to each of the textFileds, the other way you could do it is to set and track a tag number for identifing each UITextField
. This would also work, by the way, with any UIView object like buttons, image etc. Adding a tag to something also allows you to get a reference to that object by calling [self.view viewWithTag:MY_DEFINED_TAG_ID];
which can come in handy.
In this case you would start by going in the xib file where you dragged the UITextField into the window and set each field to it's own Tag # that you can use to test the id of the field from your method. You will find a Tag field in the Attributes Inspector.
In addition to this, you could, in your .h file do a define for the tags make it easier to keep track of them. This define list could go in either yourClass.h file or a Constants.h if you have such a file in which case just make sure you #import Constants.h into yourClass.h file. I know there are other places you could define all this but that is two.
#define TEXTFIELD1_TAG 900
#define TEXTFIELD2_TAG 901
#define TEXTFIELD3_TAG 902
#define TEXTFIELD4_TAG 903
If you are going to use – textFieldShouldBeginEditing:
or any delegate method, make sure you make your class a UITextFieldDelegate
. In yourClass.h (I'll assume it's a viewController) you will need a line like this:
@interface My_ViewController : UIViewController < UITextFieldDelegate >
Then you will be able to use any of these methods in your view to track the textFields:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
– textField:shouldChangeCharactersInRange:replacementString:
– textFieldShouldClear:
– textFieldShouldReturn:
But there are other options for the placement of your switch/case code depending on your workflow. If you look in you xib file you will find there are lots of events associated with your textfield that could fire any custom method you write. For example you could hook up the TouchDown event to trigger a custom method like the one below which has the switch/case code you could put anywhere you need it.
In this method I am using the #define we saw above to check the values of the tag. This way, I don't have to remember the tag number, I can just remember TEXTFIELD1_TAG
which is much easier.
- (IBAction)textFieldHasBeenTouched:(id)sender {
int textFieldTag = sender.tag
switch (textFieldTag)
{
case TEXTFIELD1_TAG:
// TextField 1
break;
case TEXTFIELD2_TAG:
// TextField 2
break;
case TEXTFIELD3_TAG:
// TextField 3
break;
case TEXTFIELD4_TAG:
// TextField 4
break;
default:
break;
}
}
Or use one of the delegate methods. In this method I assumed you didn't use the #define to help track tags. But since you set them in the xib, you know what they are.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
switch ([textField tag])
{
case 900:
// TextField 1
break;
case 901:
// TextField 2
break;
case 902:
// TextField 3
break;
case 903:
// TextField 4
break;
default:
break;
}
return YES;
}