How to get UITextField Tap Event?

后端 未结 5 1124
既然无缘
既然无缘 2020-12-18 18:42

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 it

相关标签:
5条回答
  • 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!

    0 讨论(0)
  • 2020-12-18 18:59

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

     [textField1 addTarget:delegate action:@selector(textField1Active:) forControlEvents:UIControlEventEditingDidBegin];
    
    0 讨论(0)
  • 2020-12-18 19:02

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 19:19

    Connect TextField with delegate and Now calling this function!!

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {       
       textView.text=@"   ";
       return YES;
    }
    
    0 讨论(0)
  • 2020-12-18 19:22

    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
    }
    
    0 讨论(0)
提交回复
热议问题