i want to restrict user from entering space in a UITextField. for this i m using this code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ( string == @" " ){
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You have entered wrong input" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[error show];
return NO;
}
else {
return YES;
}
}
but it is not working .... what is wrong in it ?
The problem is
string == @" "
is wrong. Equality for strings is done using:
[string isEqualToString:@" "]
:).
This will search to see if your replacement string contains a space, if it does then it throws the error message up, if it doesn't it returns YES.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSRange spaceRange = [string rangeOfString:@" "];
if (spaceRange.location != NSNotFound)
{
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You have entered wrong input" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[error show];
return NO;
} else {
return YES;
}
}
The current answer is this:
Set the View Controller to conform to the UITextFieldDelegate (should look something like this near the top of your code):
@interface YourViewController () <UITextFieldDelegate>
...
@ end
@ implementation YourViewController
...
@end
Then make the textfield use the View Controller as its delegate. Do this by going to the Interface Builder, control clicking on the textfield and dragging a line to the yellow circle on the bar underneath the View Controller, and selecting "delegate" from the menu that pops up. You could alternatively do this in code by setting the delegate after making the property described in the next paragraph. Do it this way with self.yourTextField.delegate = self; in an appropriate place, possibly in viewDidLoad.
Also set the textField up as a property on the View Controller. Do this by going to the Interface Builder, with its code open in the assistant editor, and control click and drag from the text field in the Interface Builder, to the place in the code where the properties are listed (between @interface and the first @end). Then enter a name in the pop up window. In the code below I used "yourTextField" for example. (you can skip this section, together with the outside if loop in the code below if you are sure that this is the only text field that will use the View Controller as its delegate, but it is best to plan ahead for future possibilities)
Then you can disallow spaces from even be entered using the following delegate method:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.yourTextField)
{
if ([string isEqualToString:@" "] )
{
return NO;
}
}
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With