user3114750 user3114750 - 9 days ago 6
iOS Question

How to add some text after end editing UITextfield in iOS

I want to add some strings after end editing in

UITextField
.

Example :


textfield 1 : user input 1000 . After focus to textfield2,I want to add "miles" to textfield1. => textfield1 : 1000 miles

And if user focus to textfield1 again, it shows only 1000 for user edit, do not have "miles".


I tried but it not work

- (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))
{
....
}
}

hpp hpp
Answer

please try this,

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

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

here textFieldDidEndEditing is textField's delegate method. you need to set textfield1.delegate=self. hope this code will help you.

Comments