I know that there is a lot of info on the Internet about that question but I am stuck. I have app in which I need to customise my UITextFields
. For example I need change border colour when edit starts or I need limitation of how many characters I can set in different text fields. I also need an image in several text fields which has to be indented on the left. To do all this I decided to make custom UITextField class (to subclass UITextField). I created new class from UITextField
(which implements <UITextFieldDelegate>
). In that class I use self.delegate = self
(this is widely used on the internet and people say it is working) so I can implement shouldChangeCharactersInRange
or textFieldShouldBeginEditing
inside my custom class. My problem is that in this configuration I receive infinite loop and App restart (see my question about that). This comes from self.delegate = self
. I understand that in some cases I can use observer
but in that case how I can implement shouldChangeCharactersInRange
inside my class?
If I don't implement my class in that way and delegate my text field to my view controller. I have to implement all that methods in my view controller class which in my opinion is very ugly solution.
So my question is how properly implement UITextField subclass?
P.S. I suppose that I do it in the wrong way but I can not figure out which is the proper one.
EDIT:
Here is my code:
MyCustomTextField.h
@interface MyCustomTextField : UITextField
@property (nonatomic) int maxSymbols;
@property (nonatomic) int leftIndent;
@end
MyCustomTextField.m
@interface MyCustomTextField () <UITextFieldDelegate>
@end
@implementation MyCustomTextField
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
self.delegate = self;
self.clipsToBounds = YES;
[self setLeftViewMode:UITextFieldViewModeAlways];
UIImageView *imageView1 = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 37, 20)];
imageView1.image = [UIImage imageNamed:@"otp_back"];
self.leftView = imageView1;
}
return self;
}
- (CGRect) leftViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super leftViewRectForBounds:bounds];
textRect.origin.x = 5;
return textRect;
}
also this method for checking max length:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Restrict number of symbols in text field to "maxSymbols"
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;
return newLength <= (int)_maxSymbols || returnKey;
}
When I go to the text field and start to type from virtual keyboard of the simulator I receive infinite loop and exit with BAD ACCESS. It it very strange, because if the keyboard is of type numeric or password or if I type from Mac keyboard I did not receive the issue.