i want to restrict user from entering space in a UITextField. for this i m using this code
我想限制用户在UITextField中输入空格。为此,我使用此代码
- (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 ?
但它不起作用......它有什么问题?
6 个解决方案
#1
10
The problem is
问题是
string == @" "
is wrong. Equality for strings is done using:
是错的。字符串的平等使用:
[string isEqualToString:@" "]
:).
#2
9
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.
这将搜索您的替换字符串是否包含空格,如果是,则抛出错误消息,如果不是则返回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;
}
}
#3
4
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):
将View Controller设置为符合UITextFieldDelegate(在代码顶部附近应该看起来像这样):
@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.
然后使文本字段使用View Controller作为其委托。通过转到界面生成器,控制单击文本字段并将一条线拖到视图控制器下方栏上的黄色圆圈,然后从弹出的菜单中选择“委托”来执行此操作。您可以在代码中通过在创建下一段中描述的属性后设置委托来执行此操作。用self.yourTextField.delegate = self这样做吧;在适当的地方,可能在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)
还将textField设置为View Controller上的属性。这样做是通过转到Interface Builder,在助理编辑器中打开代码,然后控制从Interface Builder中的文本字段单击并拖动到列出属性的代码中的位置(在@interface和第一个之间) @结束)。然后在弹出窗口中输入名称。在下面的代码中,我使用了“yourTextField”。 (如果您确定这是唯一将使用View Controller作为其委托的文本字段,您可以跳过本节以及下面代码中的外部if循环,但最好提前计划以备将来使用)
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;
}
#4
2
Try this (set this in your - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string):
试试这个(在你的 - (BOOL)textField中设置它:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string):
NSArray *escapeChars = [NSArray arrayWithObjects:@" ", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"",nil];
int len = [escapeChars count];
NSMutableString *temp = [[textField text] mutableCopy];
for(int i = 0; i < len; i++) {
[temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
}
[textField setText:temp];
return TRUE;
#5
1
string == @" "
Isn't that just going to compare the adress of each of string
and @" "
? Thats not the comparison you want to do.
这不仅仅是比较每个字符串和@“”的地址吗?那不是你想做的比较。
Also do you want to prevent them from entering a string that is just a space? If so then you need to change that ==
into a proper string comparison and you are good to go. If not and you want to prevent all spaces in an input string then you need a string matcher
你还想阻止他们输入一个只是空格的字符串吗?如果是这样,那么你需要将==更改为正确的字符串比较,你就可以了。如果没有,并且您想要阻止输入字符串中的所有空格,那么您需要一个字符串匹配器
#6
0
Below is what I am using for password and confirm password
以下是我用于密码和确认密码的内容
In PrefixHeader.pch
file add below
在PrefixHeader.pch文件中添加如下
#define NONACCEPTABLE_PASSWORD_CHARACTERS @" "
And in code use below.
并在下面的代码使用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==passwordTF || textField==confirmPasswordTF) {
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:NONACCEPTABLE_PASSWORD_CHARACTERS];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}
#1
10
The problem is
问题是
string == @" "
is wrong. Equality for strings is done using:
是错的。字符串的平等使用:
[string isEqualToString:@" "]
:).
#2
9
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.
这将搜索您的替换字符串是否包含空格,如果是,则抛出错误消息,如果不是则返回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;
}
}
#3
4
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):
将View Controller设置为符合UITextFieldDelegate(在代码顶部附近应该看起来像这样):
@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.
然后使文本字段使用View Controller作为其委托。通过转到界面生成器,控制单击文本字段并将一条线拖到视图控制器下方栏上的黄色圆圈,然后从弹出的菜单中选择“委托”来执行此操作。您可以在代码中通过在创建下一段中描述的属性后设置委托来执行此操作。用self.yourTextField.delegate = self这样做吧;在适当的地方,可能在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)
还将textField设置为View Controller上的属性。这样做是通过转到Interface Builder,在助理编辑器中打开代码,然后控制从Interface Builder中的文本字段单击并拖动到列出属性的代码中的位置(在@interface和第一个之间) @结束)。然后在弹出窗口中输入名称。在下面的代码中,我使用了“yourTextField”。 (如果您确定这是唯一将使用View Controller作为其委托的文本字段,您可以跳过本节以及下面代码中的外部if循环,但最好提前计划以备将来使用)
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;
}
#4
2
Try this (set this in your - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string):
试试这个(在你的 - (BOOL)textField中设置它:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string):
NSArray *escapeChars = [NSArray arrayWithObjects:@" ", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"",nil];
int len = [escapeChars count];
NSMutableString *temp = [[textField text] mutableCopy];
for(int i = 0; i < len; i++) {
[temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
}
[textField setText:temp];
return TRUE;
#5
1
string == @" "
Isn't that just going to compare the adress of each of string
and @" "
? Thats not the comparison you want to do.
这不仅仅是比较每个字符串和@“”的地址吗?那不是你想做的比较。
Also do you want to prevent them from entering a string that is just a space? If so then you need to change that ==
into a proper string comparison and you are good to go. If not and you want to prevent all spaces in an input string then you need a string matcher
你还想阻止他们输入一个只是空格的字符串吗?如果是这样,那么你需要将==更改为正确的字符串比较,你就可以了。如果没有,并且您想要阻止输入字符串中的所有空格,那么您需要一个字符串匹配器
#6
0
Below is what I am using for password and confirm password
以下是我用于密码和确认密码的内容
In PrefixHeader.pch
file add below
在PrefixHeader.pch文件中添加如下
#define NONACCEPTABLE_PASSWORD_CHARACTERS @" "
And in code use below.
并在下面的代码使用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==passwordTF || textField==confirmPasswordTF) {
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:NONACCEPTABLE_PASSWORD_CHARACTERS];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}