【问题标题】:Custom Keyboard putting text in multiple text boxes自定义键盘将文本放入多个文本框中
【发布时间】:2012-07-28 22:13:58
【问题描述】:

我觉得问这个问题很糟糕。我在 SO 上看过几次,但我不明白答案。我有一个在单独视图中制作的自定义键盘。它适用于一个文本框。但是现在我想要多个文本框,键盘将在其中输入数字。我已将视图控制器设置为文本字段委托,当您输入任一文本框时,我的自定义键盘就会弹出。但是,我必须设置数字的代码仅适用于其中一个文本框。所以我想检查一下选择了哪个文本框,所以我将在那个框中进行编辑。我使用了方法 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 并尝试了以下代码...

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

currentTextField 是一个 UItextField。然后我想在下面的代码中替换 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;
}
}

但是,编译器不喜欢我的行 [self.numberOne = currentTextField];我尝试遵循其他一些建议,但不幸的是我不明白他们在说什么。有没有人给我新手指导?提前非常感谢。

我在宽敞的帮助下修复了编译器问题。谢谢。我的 textFieldShouldBeginEditing 方法现在是...

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

我将 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;
}
}

现在发生了一些奇怪的事情。当我单击第一个文本字段时,键盘出现但没有任何反应,然后我点击第二个文本字段并在第一个文本字段中开始编辑。如果我单击第一个文本字段,编辑将从第二个开始。任何帮助将不胜感激!

【问题讨论】:

    标签: ios xcode textbox keyboard


    【解决方案1】:

    好吧,我可以回答你的编译器问题-

    要分配对 currentTextField 的引用,它应该是:

    currentTextField = self.numberOne;

    发送消息时使用括号(调用对象的方法)。

    你把你的作业用括号括起来了,这个作业是错误的。

    (您目前正在使用 currentTextField 覆盖您对 textField 的引用)

    我需要查看更多代码才能进一步诊断!祝你好运

    编辑:(这些是未经测试的更改,但你会明白的)

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

    【讨论】:

    • 感谢您对编译器问题的帮助。编译器更开心!但是,这并没有解决我的问题。即使我用 currentTextField 更改了 numberOne,键盘也没有将数字放在任何文本框中。尽管当我输入任何文本字段时仍会调用我的键盘。您还需要查看哪些其他代码?让我知道。再次感谢。
    • 如果你已经合成了 currentTextField 使用 self.currentTextField = self.numberOne;您还需要在更改 firstResponder 时将 self.userIsEnteringANumber 重置为 NO...您可以省略 self.currentTextField.text = self.currentTextField.text;
    • 是的,我已经合成了currentTextField。我用了你所有的技巧,谢谢你的编辑。但是对面的文本字段编辑的问题仍然存在。关于那个有什么想法!?
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2011-03-16
    • 2023-04-11
    • 2011-03-18
    • 2016-01-09
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多