方法一:添加代理 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *tem ...
方法一:添加代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
if (![string isEqualToString:tem]) {
return NO;
}
return YES;
}
方法二:
[self.searchTextField addTarget:self action:@selector(searchTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
在searchTextFieldChange方法中 直接替換空的字元串
- (void)searchTextFieldChange:(UITextField *)textField{
textField.text =[textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
}