Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add button to uitextfield

When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks

enter image description here

like image 772
dotnet-practitioner Avatar asked Jan 18 '13 05:01

dotnet-practitioner


People also ask

How do I get the Done button on my Iphone keyboard Swift?

addDoneKeyboardButton() — creates the keyboard done button using UIToolbar. Inside the toolbar, we create a UIBarButtonItem. You can name the button however you want, and you can add multiple buttons (depending on your needs). In other words, use this function to customize the toolbar.

What is a text field on Iphone?

A text field is a UI element that enables the app to get user input.


1 Answers

You can use the below code for this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  if(textField == yourFirstTextField)
  {
      UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
      [addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];
      [addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
      textField.rightViewMode = UITextFieldViewModeWhileEditing;
      textField.rightView = addButton;
  }
}

You can use the above method if you added the field in IB.

If you are created it through code, you need to add the below code when creating:

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
like image 182
Midhun MP Avatar answered Oct 19 '22 08:10

Midhun MP