-->

Custom UITextField delegate set to self initiates

2020-04-17 04:43发布

问题:

I am writing iPhone App in which I need custom UITextField class. For my text fields I need indent, image inside before the text and max characters. For this reason I created custom class based on UITextField. All my text fields will be based on this new class. I use Google and searched Stackoverflow and I find that in cases like mine I have to use self.delegate = self; during the init so I don't need to implement methods like textFieldShouldBeginEditing or textFieldShouldEndEditing inside my View Controller class. All my text fields will be created in storyboard so initWithCoder. As a result I receive infinite loop and App crash (I use simulator at this moment) after typing 1, 2 or 3 symbols. It is interesting that for numeric keyboard or password keyboard there is no such issue. Also if I type symbols on my Mac keyboard instead on the simulator there is no problem. I tried to debug but during the crash it jumps directly into loop and exist with error. How to overcome this problem?

P.S. I asked question when I receive infinite loop and there was comments that self.delegate = self; can cause such loop, but I see that this statement is widely used. May be I did not do this correctly, but I can not figure out how to make reusable class out of UITextField.

EDIT: Here is my code:

On inti I initialize and set border color:

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {

        self.layer.borderColor=[[UIColor blackColor] CGColor];
        self.delegate = self;
    }
    return self;
}

When I start edit I change border color and set indent (indent set will be moved in property setter):

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    // Change border of text field that is editing to orange
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor orangeColor] CGColor];
    textField.layer.borderWidth= 1.0f;

    UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _leftIndent, 10)];
    [self setLeftViewMode:UITextFieldViewModeAlways];
    [self setLeftView:spacerView];

    return YES;
}

On finish editing I return back color:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    // Change border of text field that is edited to black
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor blackColor] CGColor];
    textField.layer.borderWidth= 1.0f;

    return YES;
}

And on value change I check max characters:

- (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;
}

回答1:

I don't know if this can be actual answer, but I have work around which is the answer of my next question How properly subclass UITextField in iOS? (from justafinger). I don't make assignment self.delegate = self; in my custom control, but I have methods that are called from delegates in view controller.

I hope this can help people with problem like mine.