0

我刚刚得到了一个自定义键盘。我想对应用程序进行最后一次触摸,但我无法弄清楚。我想限制文本输入的数量。我用过这个方法......

- (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;
}

但它永远不会被调用。我可以在我的实际键盘上输入,文本限制为 8 个。但如果我在模拟器中使用我的自定义键盘,我可以永远输入数字。知道如何从我的自定义键盘调用此方法吗?谢谢!!

编辑

响应 Kjuly 的帮助。这是我的更多代码。

- (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;
}
}

开始编辑时会调用此方法。但仍然只有在我使用键盘时才会调用 shouldChangeChararcterInRange 方法。我的 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;
}

所以感谢大家的帮助,我希望尽快解决这个问题!

4

2 回答 2

0

但它永远不会被调用。

分配后添加yourTextField

[self.yourTextField setDelegate:self];

编辑

你的-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;
}
于 2012-07-31T01:51:01.630 回答
0

嗯,我终于想通了!!我发现一个帖子说 Xcode 在使用自定义键盘时不会运行方法 shouldChangeCharactersInRange 。与 Xcode 有关,不知道按钮上是否有一个或多个项目。所以这就是为什么从未调用过该方法的原因。然后我看到有人试图用他创建的另一种方法来限制文本。这对我不起作用,因为我的自定义键盘会检查小数以及附加数字。所以我想出了以下代码,它可以工作!我的输入限制为 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;
    }
}

所以感谢大家的帮助,尤其是@Kjuly。

于 2012-08-01T00:04:22.100 回答