How to get UITextField Tap Event?

谁说我不能喝 提交于 2019-12-01 02:47:06

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

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

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

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
}

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!

Connect TextField with delegate and Now calling this function!!

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {       
   textView.text=@"   ";
   return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!