How to get UITextField Tap Event?

2019-02-16 05:52发布

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

5条回答
做自己的国王
2楼-- · 2019-02-16 06:39

Connect TextField with delegate and Now calling this function!!

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {       
   textView.text=@"   ";
   return YES;
}
查看更多
Emotional °昔
3楼-- · 2019-02-16 06:43

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
}
查看更多
家丑人穷心不美
4楼-- · 2019-02-16 06:49

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!

查看更多
We Are One
5楼-- · 2019-02-16 06:51

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;
}
查看更多
小情绪 Triste *
6楼-- · 2019-02-16 06:54

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

 [textField1 addTarget:delegate action:@selector(textField1Active:) forControlEvents:UIControlEventEditingDidBegin];
查看更多
登录 后发表回答