如果同时设置了UISearchBarDelegate和UITextFieldDelegate,并且设置的允许输入的字符串,则需要在下面方法中,允许\n输入。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
完整代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([StringUtil isInputRuleNotBlank:string] || [string isEqualToString:@""] || [string isEqualToString:@"\n"]) {
//当输入符合规则和退格键时允许改变输入框
return YES;
}
else {
return NO;
} }