I have two textfield. Using this It works great to set the max character to textfield. How I can check what textfield is been used, in order to set differents max lenghts?
Asked
Active
Viewed 2,274 times
1 Answers
2
I think you are trying to set different maximum character lengths for your textfields, then first of all, create outlet for your textfield in header,
//header
IBOutlet UTtextField *textField1;
IBOutlet UITextField *textField2;
Hope you have already done it.
Then just check the textField in the implementation method, I will modify the answer with:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == textField1) {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 25) ? NO : YES;
}
else if(textField == textField2)
{
//do the same with different values
}
}
EDIT You can also set tags for your TextFields and make use of it, like switch(textField.tag)
.

DD_
- 7,230
- 11
- 38
- 59
-
Or you could set tags for the textfields and compare the tags. – Fahri Azimov Apr 16 '13 at 13:16
-
Yep, sure, but for what you have suggested, there always should be instance variable or property set for the textfield. :) – Fahri Azimov Apr 16 '13 at 13:22
-
@FahriAzimov hm..ya...is it such a heavy task? Don't think so. – DD_ Apr 16 '13 at 13:24
-
I'm not saying that your approach is wrong, I'm just saying that there is also another way. And, sometimes we need temporary UI items, for those, my way would be better approach. – Fahri Azimov Apr 16 '13 at 13:30