Is there a way to only allow the user enter one word in the UITextField? If so, how?
Here’s the current code that I’m using:
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
_disallowedCharacters = [NSCharacterSet whitespaceCharacterSet];
for(int i = 0; i < [_searchBox.text length]; i++){
unichar c = [_searchBox.text characterAtIndex:i];
if (![_disallowedCharacters characterIsMember:c])
{
return NO;
}
}
return YES;
}
Use the
UITextFieldDelegateprotocol:where
disallowedCharactersis anNSCharacterSetcontaining the characters that should not be accepted by the keyboard, i.e. a space, punctuation, numbers, etc: