简体   繁体   English

自定义键盘将文本放入多个文本框中

[英]Custom Keyboard putting text in multiple text boxes

I feel bad asking this question. 问这个问题我感到很难过。 I have seen it on SO a couple of times, but I am not understanding the answers. 我已经看过几次了,但是我不明白答案。 I have a custom keyboard that I made in a separate view. 我有一个在单独视图中制作的自定义键盘。 It worked great for one text box. 它非常适合一个文本框。 But now I want to have multiple text boxes that the keyboard will enter numbers in. I have made the view controller the textfield delegate and my custom keyboard pops up when you enter either of the text boxes. 但是现在我希望有多个文本框,键盘将在其中输入数字。我已经将视图控制器设置为文本字段委托,并且当您输入两个文本框时,都会弹出我的自定义键盘。 However, the code that I have to set the numbers is only for one of the text boxes. 但是,我必须设置数字的代码仅适用于其中一个文本框。 So I wanted to check to see which text box was selected so I will be editing in that box. 因此,我想检查一下选择了哪个文本框,以便在该框中进行编辑。 I used the method - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField and tried the following code... 我使用了方法-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField并尝试了以下代码...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder]) 
{
    [self.numberOne = currentTextField];    
}
else if ([numberTwo isFirstResponder])
{
    [self.numberTwo = currentTextField];
}    
}

the currentTextField is a UItextField. currentTextField是一个UItextField。 I then wanted to substitute currentTextField in the following code to replace numberOne. 然后,我想在以下代码中替换currentTextField来替换numberOne。

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

However, the complier doesn't like my line [self.numberOne = currentTextField]; 但是,编译器不喜欢我的代码行[self.numberOne = currentTextField]; I tried following some of the other advice, but unfortunately I don't understand what they are saying. 我尝试遵循其他一些建议,但不幸的是,我不明白他们在说什么。 Does anyone have newbie directions for me? 有人对我有新手指示吗? Thanks so much in advance. 非常感谢。

I have fixed the compiler issue with the help from spacious. 我已经在宽敞的帮助下修复了编译器问题。 Thanks. 谢谢。 My textFieldShouldBeginEditing method is now... 我的textFieldShouldBeginEditing方法现在是...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder]) 
{
    self.currentTextField = self.numberOne;    
}
else if ([numberTwo isFirstResponder])
{
    self.currentTextField = self.numberTwo;
}
return YES;
} 

and I changed the IBAction buttonPressed to the following... 我将IBAction buttonPressed更改为以下内容...

- (IBAction)buttonPressed:(UIButton *)sender 
{
NSString *button = [sender currentTitle];
NSRange decimalpoint = [self.currentTextField.text rangeOfString:@"."];
if (self.userIsEnteringANumber)
{
    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 = button;
    self.userIsEnteringANumber = YES;
}
}

And now something weird happens. 现在发生了一些奇怪的事情。 When I click on the first text field, the keyboard comes up but nothing happens, then I hit the second text field and editing begins in the first text field. 当我单击第一个文本字段时,键盘出现,但是什么也没有发生,然后我按下了第二个文本字段,并在第一个文本字段中开始编辑。 If I click on the first text field, editing begins on the second. 如果我单击第一个文本字段,则编辑将从第二个开始。 Any help would be appreciated!! 任何帮助,将不胜感激!!

Well, I can answer your compiler question- 好吧,我可以回答您的编译器问题-

To assign a reference to currentTextField, it should be: 要分配对currentTextField的引用,应为:

currentTextField = self.numberOne; currentTextField = self.numberOne;

Brackets are used when sending messages (calling methods on objects). 发送消息时使用括号(对象的调用方法)。

You wrapped your assignment in brackets and the assignment is the wrong way. 您将作业放在方括号中,而作业是错误的方法。

(You'd currently be overwriting your references to your textFields with currentTextField) (您当前将使用currentTextField覆盖对textField的引用)

I would need to see more code to diagnose any further! 我将需要查看更多代码来进一步诊断! Good luck 祝好运

EDIT: (these are untested changes, but you'll get the idea) 编辑:(这些是未经测试的更改,但您会明白的)

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder] ) 
{
    self.currentTextField = self.numberOne;
    self.userIsEnteringANumber = NO;
}
else if ([numberTwo isFirstResponder])
{
    self.currentTextField = self.numberTwo;
    self.userIsEnteringANumber = NO;

}    
}

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

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