【问题标题】:How to add some text after end editing UITextfield in iOS如何在 iOS 中结束编辑 UITextfield 后添加一些文本
【发布时间】:2016-11-25 19:40:46
【问题描述】:

我想在UITextField结束编辑后添加一些字符串。

例子:

文本字段 1 :用户输入 1000 。聚焦到 textfield2 后,我想在 textfield1 中添加“英里”。 => textfield1 : 1000 英里

如果用户再次关注 textfield1,它只显示 1000 供用户编辑,没有“英里”。

试过了还是不行

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([textfield1 isFirstResponder] && (textfield1 != touch.view))
    {
        textfield1.text = [NSString stringWithFormat:@"miles"];
    }
    
    if ([textfield2 isFirstResponder] && (textfield2 != touch.view))
    {
        ....
    }     
}

【问题讨论】:

标签: ios objective-c uitextfield


【解决方案1】:

你可以使用 UITextField 的委托方法来做到这一点

    - (void)textFieldDidBeginEditing:(UITextField *)textField;

    - (void)textFieldDidEndEditing:(UITextField *)textField;

伯恩哈德

【讨论】:

    【解决方案2】:

    请试试这个,

    - (void)textFieldShouldBeginEditing:(UITextField *)textField
    {
        textField.text=@"";
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        textfield1.text = [NSString stringWithFormat:@"%@ miles",textField.text];
    }
    

    这里的textFieldDidEndEditing 是textField 的委托方法。你需要设置textfield1.delegate=self。 希望此代码对您有所帮助。

    【讨论】:

    • @Krishna:这可以通过在 textFieldShouldBeginEditing 方法中清除 textField 来解决。
    【解决方案3】:

    试试这个:-

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        if(textField.tag==10) // textfield 1
        {
            if(![ _txt1.text rangeOfString:@"miles"].location!= NSNotFound)
            {
                _txt1.text = [_txt1.text stringByAppendingString:@"miles"];
            }
        }
        else if (textField.tag == 20) // textfield 2
        {
            if([ _txt1.text rangeOfString:@"miles"].location!= NSNotFound)
            {
                _txt1.text = [_txtUser1.text stringByReplacingOccurrencesOfString:@"miles" withString:@""];
            }
        }
    }
    


    注意:- 将您的文本字段的标签设置为 10 和 20。
    _txt1 是您的文本字段1 (UITextfield)

    【讨论】:

      【解决方案4】:

      text设为NSString的全局变量,然后

      - (void)textFieldDidBeginEditing:(UITextField *)textField
          {
             if(text.length>0)
             {   
                self.textField.text =text;
             }
          }
      
          - (void)textFieldDidEndEditing:(UITextField *)textField
          {
              text =self.txtField.text;
              self.txtField.text = [NSString stringWithFormat:@"%@ miles", text];
          }
      

      【讨论】:

        【解决方案5】:

        设置文本字段的 delagate,如下所示

        @interface YourController : UIViewController <UITextFeildDelegate>
        

        然后使用以下方法检查

        -(void)textFieldDidEndEditing:(UITextField *)textField{
         if(textField....) //condition to check textfeild is
          {
           //do your stuff
        
          }
        }
        

        希望对你有帮助

        【讨论】:

          【解决方案6】:

          像这样为 textField1 设置标签

            self.textField1.tag = 100;
          

          在 viewDidLoad 到你的 textField 出口,然后

            -(void)textFieldDidEndEditing:(UITextField *)textField
             {
                   if (textField.tag == 100)
                   {
                        self.txtUserId.text = [NSString stringWithFormat:@"%@ miles", self.txtUserId.text];
                   }
             }
          

          因为这是一个委托方法,它将为每个 textField 结束编辑事件触发,这就是您必须检查标签号的原因。并且不要忘记设置委托

             @interface YourController : UIViewController <UITextFeildDelegate>
          

          【讨论】:

            猜你喜欢
            • 2014-03-13
            • 1970-01-01
            • 2018-06-01
            • 1970-01-01
            • 2020-07-24
            • 2012-03-29
            • 1970-01-01
            • 2020-08-05
            • 1970-01-01
            相关资源
            最近更新 更多