如何在iOS中结束编辑UITextfield后添加一些文本

如何在iOS中结束编辑UITextfield后添加一些文本,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我想在UITextField中结束编辑后添加一些字符串 例如: 文本字段1:用户输入1000。在关注文本字段2之后,我想在文本字段1中添加“英里”。=>textfield1:1000英里 如果用户再次聚焦到文本字段1,它只显示1000供用户编辑,没有“英里” 我试过了,但没用 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event { UITouch *touch = [[event allTouches] an

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

例如:

文本字段1:用户输入1000。在关注文本字段2之后,我想在文本字段1中添加“英里”。=>textfield1:1000英里

如果用户再次聚焦到文本字段1,它只显示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))
    {
        ....
    }     
}

可以使用UITextField的委托方法执行此操作

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


Bernhard

您可以使用UITextField的委托方法来执行此操作

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

伯恩哈德请试试这个

- (void)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.text=@"";
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    textfield1.text = [NSString stringWithFormat:@"%@ miles",textField.text];
}
这里
textfielddidediting
是textField的委托方法。您需要设置
textfield1.delegate=self
。 希望此代码对您有所帮助。

请尝试此代码

- (void)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.text=@"";
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    textfield1.text = [NSString stringWithFormat:@"%@ miles",textField.text];
}
这里
textfielddidediting
是textField的委托方法。您需要设置
textfield1.delegate=self

希望这段代码能对您有所帮助。

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

@interface YourController : UIViewController <UITextFeildDelegate>

希望这有助于设置文本字段的delagate,如下所示

@interface YourController : UIViewController <UITextFeildDelegate>
希望这有帮助

试试这个:-

- (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。
\u txt1
是您的文本字段1(
UITextfield

试试这个:-

- (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。
\u txt1
是文本字段1(
UITextfield

使
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];
    }

使
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];
    }

像这样为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 end edit事件触发,这就是为什么您必须检查标记号。别忘了设置代理

   @interface YourController : UIViewController <UITextFeildDelegate>
@接口YourController:UIViewController

像这样设置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 end edit事件触发,这就是为什么您必须检查标记号。别忘了设置代理

   @interface YourController : UIViewController <UITextFeildDelegate>
@接口YourController:UIViewController

查看查看@Krishna:这可以通过清除textFieldShouldBeginEditing方法中的textField来解决。@Krishna:这可以通过清除textFieldShouldBeginEditing方法中的textField来解决。