将NSString限制为数字和#(如果输入了字符). (包括代码)

问题描述:

我已经在网站上搜索了我的答案, 但是我认为这也是我陷入这个问题的方式.

I have searched the site for my answer, however I think it is also how I got myself into this problem.

我正试图将我的1个唯一文本字段限制为10个字符和唯一数字.

I'm trying to limit my 1 and only text field to 10 characters and only numbers.

以下是该网站上其他问题的大部分代码. 我想做的就是将这段代码混在一起以达到我的极限

Below is code mostly off other questions on this site. What I am trying to do is mash this code together for my limits

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:filtered]; 
    return !([newString length] > 10);
}

在我单击文本框之前,我的构建没有任何问题...但是我得到一个绿色箭头警告,指出线程在断点1处停止" 我现在已经尝试了一些不同的方法,但是现在感觉很沮丧,我希望这对这里的人来说真的很容易...

There are no problems with my build until I click my textfield... but I get a green arrow warning stating " thread stopped at breakpoint 1 " I've tried a few different things now, but now feel I am stumped, I'm hoping this is something real easy for someone here...

它们将与下面的代码单独(一个或另一个)一起工作,但我似乎无法使它们一起工作.

they will work individually (one or the other) with the code below, but I can't seem to get them working together.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:string];
    return !([newString length] > 10);
}

这会将我的输入限制为10 和...

This will limit my input to 10 and ...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

这也将我的输入仅限制为数字,没有问题. 如果我将法律定义为仅数字...

This also restricts my input to numbers only with no problems. provided I defined LEGAL as only numbers...

我似乎无法让他们一起工作... 有人可以帮我吗?

I just can't seem to get them to work together... Can anyone help me with this one ???

有很多方法可以做到这一点,但是我认为这可能是最容易理解的:

There are many ways to do this, but I think this might be the most understandable:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // Check for non-numeric characters
    NSUInteger lengthOfString = string.length;
    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
        unichar character = [string characterAtIndex:loopIndex];
        if (character < 48) return NO; // 48 unichar for 0
        if (character > 57) return NO; // 57 unichar for 9
    }
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    if (proposedNewLength > 10) return NO;
    return YES;
}

如果在新字符串的任何位置发现一个不为零的字符,则第一部分本质上将检查非空字符串是否位于"0"和"9"之间,从而拒绝编辑.第二个参数确定编辑完成后文本的长度,如果较高则与10进行比较,拒绝编辑.如果该功能通过了所有这些测试,则允许进行编辑.

The first section essentially check a non-empty string for characters that are not between '0' and '9' if it finds one anywhere in the new string it rejects the edit. The second determines what the length of the text will be if the editing is completed and compares to 10 if it's to high it rejects the edit. If the function passes all those tests then the edit is allowed.

在我单击文本字段之前,我的构建没有任何问题...但是 我收到绿色箭头警告,提示" thread stopped at breakpoint 1 "

There are no problems with my build until I click my textfield... but I get a green arrow warning stating " thread stopped at breakpoint 1 "

您显然已经在textField:shouldChangeCharactersInRange:replacementString:方法中设置了一个断点.如果您不熟悉断点,它们将如下所示:(行号列上的蓝色箭头)并且您可以使用右侧按钮来切换是否启用它们:

You have apparently set a breakpoint in your textField:shouldChangeCharactersInRange:replacementString: method. If your not familiar with breakpoints they look like this: (The blue arrow on the row numbers column) And you can toggle whether or not they are enabled with the button pictured on the right:

断点非常方便,我建议您学习使用它们.但是现在,只需单击断点并将其从列中拖出即可,就像停靠区中的图标一样,或将其关闭即可.

Breakpoints are very handy, and I suggest you learn to use them. But for now simply click on the breakpoint and drag it from the column, like an icon from the dock, or toggle that option off.