Character limit for multiple uitextfields

江枫思渺然 提交于 2019-12-06 22:07:33

Ok so the issue here is that the object which contains this code is the delegate to both textfields. Two ways to solve it. Either dont add the textfield you dont care about the length of as a delegate or the better way would be to differentiate between the fields during the shouldChangeCharactersInRange method.

For the second part of your question- range is the character(s) that is/are being changed in the text field and consists of: The position of the string to change, the length of the string being changed.

My personal preference is to say if the text length is greater than max length and your not deleting then refuse the change.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if(textField == self.textfieldYouWantToLimitTheLengthOf){

        if(textField.text.length > 2 && ![string isEqualToString:@""]){

            return NO;
        }
    }
    return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!