我刚开始使用自定义键盘。我想在应用程序上进行最后一次触摸,我无法弄明白。我想限制文本输入的数量。我用过这个方法......
- (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;
}
非常感谢所有的帮助,我希望很快能解决这个问题!
答案 0 :(得分: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;
}
答案 1 :(得分: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。