iOS----------输入框UITextField禁止输入空格

方法一:添加代理

- (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:@""];

 

 }

 

posted @ 2017-12-19 09:17  iOS张文权  阅读(3386)  评论(0编辑  收藏  举报