简体   繁体   English

使用Xcode中的自定义键盘来限制UITextfield中的文本

[英]Limit Text in UITextfield using a custom keyboard in Xcode

I just got a custom keyboard working. 我刚刚有了一个自定义键盘。 I wanted to put one last touch on the App and I can't figure it out. 我想对App进行最后一触,但我无法弄清楚。 I want to limit the amount of text input. 我想限制文本输入的数量。 I have used the method.... 我已经使用了该方法。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"user is entering numbers");
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 8) ? NO : YES;
}

But it never gets called. 但是它永远不会被调用。 I can type on my actual keyboard and the text is limited to 8. But if I use my custom keyboard in the simulator, I can put numbers in forever. 我可以在实际的键盘上键入,并且文本限制为8个。但是,如果我在模拟器中使用自定义键盘,则可以永久输入数字。 Any idea how to get this method called from my custom keyboard? 知道如何从我的自定义键盘调用此方法吗? Thanks!! 谢谢!!

EDIT 编辑

In response to Kjuly's help. 为了回应Kjuly的帮助。 Here is some more of my code. 这是我的更多代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"textFieldDidBeginEditing");
if ([fahrenheit isFirstResponder]) 
{
    self.currentTextField = self.fahrenheit;
    celcius.text = @"";
    kelvin.text = @"";
    self.userIsEnteringANumber = NO;
}
else if ([celcius isFirstResponder])
{
    self.currentTextField = self.celcius;
    fahrenheit.text = @"";
    kelvin.text = @"";
    self.userIsEnteringANumber = NO;
}
else if ([kelvin isFirstResponder])
{
    self.currentTextField = self.kelvin;
    fahrenheit.text = @"";
    celcius.text = @"";
    self.userIsEnteringANumber = NO;
}
}

This method does get called when editing begins. 编辑开始时确实会调用此方法。 But still the shouldChangeChararcterInRange method only gets called if I use my keyboard. 但是,仅当我使用键盘时,shouldChangeChararcterInRange方法才被调用。 My viewDidLoad method is 我的viewDidLoad方法是

- (void)viewDidLoad
{
[super viewDidLoad];
[fahrenheit setText:@""];
[celcius setText:@""];
[kelvin setText:@""];
[fahrenheit setInputView:keyPad];
[celcius setInputView:keyPad];
[kelvin setInputView:keyPad];
fahrenheit.delegate = self;
celcius.delegate = self;
kelvin.delegate = self;
}

So thanks for all the help, I hope to get this figured out soon! 因此,感谢您的所有帮助,我希望能尽快解决这个问题!

But it never gets called. 但是它永远不会被调用。

Add this after you've alloced the yourTextField : 在分配了yourTextField之后添加以下yourTextField

[self.yourTextField setDelegate:self];

EDIT 编辑

Your -viewDidLoad: method should like: 您的-viewDidLoad:方法应为:

- (void)viewDidLoad
{
  [super viewDidLoad];

  // you should alloc your text fields here or in |-init| method
  fahrenheit = [[UITextField alloc] initWithFrame:CGRectMake(...)];
  celcius    = [[UITextField alloc] initWithFrame:CGRectMake(...)];
  kelvin     = [[UITextField alloc] initWithFrame:CGRectMake(...)];
  [self.view addSubview:fahrenheit];
  [self.view addSubview:celcius];
  [self.view addSubview:kelvin];

  [fahrenheit setText:@""]; // you can set it to nil
  [celcius setText:@""];
  [kelvin setText:@""];
  [fahrenheit setInputView:keyPad]; // what's keyPad? It's your custom input view, uh?
  [celcius setInputView:keyPad];
  [kelvin setInputView:keyPad];
  fahrenheit.delegate = self;
  celcius.delegate = self;
  kelvin.delegate = self;
}

Well, I finally figured it out!! 好吧,我终于想通了! I found a post that said Xcode won't run the method shouldChangeCharactersInRange when using a custom keyboard. 我发现有一条帖子说,使用自定义键盘时,Xcode不会运行shouldChangeCharactersInRange方法。 Something to do with Xcode not knowing if a button had one item on it or several. 与Xcode有关的是,它不知道按钮上有一个还是多个。 So that's why that method never was called. 这就是为什么从未调用过该方法的原因。 I then saw someone try to limit the text with another method that he created. 然后我看到有人试图用他创建的另一种方法来限制文本。 This didn't work for me, because my custom keyboard, checks for decimals along with appending the number. 这对我不起作用,因为我的自定义键盘会检查小数点并附加数字。 So I came up with the following code and it works! 所以我想出了以下代码,它可以正常工作! My inputs are limited to 8 characters, I can still use my neg sign because that was a different method. 我的输入限制为8个字符,我仍然可以使用负号,因为那是另一种方法。

- (IBAction)buttonPressed:(UIButton *)sender 
{
NSString *button = [sender currentTitle];
NSRange decimalpoint = [self.currentTextField.text rangeOfString:@"."];
if (self.userIsEnteringANumber)
{
    if (currentTextField.text.length < 8)
    {
       if ([button isEqualToString:@"."] && decimalpoint.location != NSNotFound)
       {
           self.currentTextField.text = self.currentTextField.text;
       }else 
           self.currentTextField.text = [self.currentTextField.text stringByAppendingString:button];
    }else 
        self.currentTextField.text = self.currentTextField.text;
}else 
    {
    self.currentTextField.text = button;
    self.userIsEnteringANumber = YES;
    }
}

So thanks for everyone's help especially @Kjuly. 因此,感谢大家的帮助,尤其是@Kjuly。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM