//添加监听方法
[textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
- (void)textFieldChanged:(UITextField*)textField{ //空格解决方案 NSString *_string = textField.text; NSRange _range = [_string rangeOfString:@" "]; if (_range.location != NSNotFound) { //有空格 NSLog(@"密码中不能包含空格"); textField.text = [NSString stringWithFormat:@"%@",[textField.text substringToIndex:textField.text.length - 1]]; } }