代码之家  ›  专栏  ›  技术社区  ›  choise

使用自定义inputView将输入返回到UITextfield

  •  0
  • choise  · 技术社区  · 14 年前

    我创建了一个UITextField和一个自定义视图。

     amountKeyboardViewController *amountKeyboard = [[amountKeyboardViewController alloc] initWithNibName:@"amountKeyboardViewController" bundle:nil];
    
    amountTextField.inputView = amountKeyboard.view;
    
    [amountTextField becomeFirstResponder];
    

    这个很好用。但是我可以用在自定义视图(UIButtons)中单击的输入填充“amountTextField”吗?

    谢谢你的提示!

    2 回复  |  直到 14 年前
        1
  •  1
  •   Reena    14 年前

    在amountTextField控制器中,需要在.h文件中编写以下代码

    @property(nonatomic,retain)UiTextField *txt; //whatever your textfield name
    

    现在在.m文件中编写以下命令

    @synthesize txt;
    

    在创建amountKeyboardViewController对象的位置编写以下语句

     amountKeyboardViewController *amountKeyboard = [[amountKeyboardViewController alloc] initWithNibName:@"amountKeyboardViewController" bundle:nil]
     amountKeyboard.objamountTextField = self;
    

    现在在amountKeyboardViewController中编写以下代码

    @class amountTextField;       //forward declaration
    

    amountTextField *objamountTextField;
    

    然后创建属性

    @property(nonatomic,retain)amountTextField *objamountTextField;
    

    现在点击按钮

    -(IBAction)clickof1
    {
        if([amountTextField.text length] <= 0)
            objamountTextField.txt.text = @"1";
        else
            objamountTextField.txt.text=[objamountTextField.txt.text stringByAppendingString:@"1"];
    }
    
        2
  •  1
  •   Reena    14 年前

    你的意思是如果你按1,那么“1”应该显示在文本字段中。

    你想要这样吗?

    您必须为所有按钮编写iAction

    -(IBAction)clickof1
    {
        if([amountTextField.text length] <= 0)
           amountTextField.text = @"1";
        else
           amountTextField.text = [amountTextField.text stringByAppendingString:@"1"];
    }
    
    -(IBAction)clickof2
    {
        if([amountTextField.text length] <= 0)
           amountTextField.text = @"2";
        else
           amountTextField.text = [amountTextField.text stringByAppendingString:@"2"];
    }
    

    像这样,你必须为你所有的按钮写iAction