How to get UITextField Tap Event?

42,420

Solution 1

Try adding a target for when a particular text field begins editing (UIControlEventEditingDidBegin):

 [textField1 addTarget:delegate action:@selector(textField1Active:) forControlEvents:UIControlEventEditingDidBegin];

Solution 2

As you are already subscribed to be UITextField delegate, implement this method:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [alert show];

    return YES;
}

Solution 3

I think it's easier if you set the delegate for the UITextField and implement the method:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

then inside that method you can easily create and show your UIAlertView.

Take a look at UITextFieldDelegate.

Good luck!

Solution 4

In Swift 3

Add a target for a particular text field for the event .editingDidBegin in viewDidLoad method

self.textField.addTarget(self, action: #selector(textFieldTouched(_:)), for: UIControlEvents.editingDidBegin)

func textFieldTouched(textField: UITextField) {
//Show AlertView
}

Solution 5

Connect TextField with delegate and Now calling this function!!

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {       
   textView.text=@"   ";
   return YES;
}
Share:
42,420

Related videos on Youtube

Azhar
Author by

Azhar

.net / iOS / Android / Windows Mobile/ React-native Optimization Optimization Optimization

Updated on July 21, 2022

Comments

  • Azhar
    Azhar almost 2 years

    I am trying to show UIAlertView on Tap or Click of UITextField for both IPad and IPhone. I make an IBAction and Attach it with Tap Down event of UITextField.

    But its not working correctly, means not always, in case of iphone and not working in-case of iPad

    - (IBAction) TopuchState
    {
        //function code
    }
    

    please help How could I do this.

    enter image description here

    • Adrian Ancuta
      Adrian Ancuta about 12 years
      Touch Up Inside? Did you tried that one?
    • Yawar
      Yawar over 7 years
      if your textfield is inside scrollview then uncheck delay content touches in inspect property for scroll view. It should work then.
  • Azhar
    Azhar about 12 years
    I have almost 8-9 UITextFields on View and in iPad control's tags are also not working if you match which UITextFields's event got fire.
  • BSKANIA
    BSKANIA about 10 years
    Thanks it saved my time.
  • Julius
    Julius almost 7 years
    I was so tired of posts asking how to dismiss the keyboard by tapping outside the textfield....thanks a bunch! (I used UIControlEventAllTouchEvents)