20

I have a UITextField that has User Interaction Disabled. So if you tap on this text field, nothing happens. Normally to check if a text field was tapped Id try the delegate methods, but I cannot because user interaction is disabled. Is there any way I can check if the text field was tapped/touched? I change another element to hidden = no; when it is tapped so I was wondering if its even possible enabling user interaction.

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • As a side-note for future visitors: If you want to open a picker to select a value for your UITextField use the picker as the text fields inputView. That will automatically display the picker instead of the keyboard when the text field is tapped. Optionally use a UIToolBar in addition as the text fields inputAccessoryView which thens gets displayed above the picker (for the done-button etc). – marsbear Sep 27 '16 at 16:00

2 Answers2

44

Best option is to turn on User Interaction and disable edit action using delegate method.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
     return NO;
} 

You can call your method inside that function to detect tap.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • 2
    Tried this too, but I have other text fields so this doesn't work. – Josue Espinosa Sep 10 '13 at 22:24
  • 3
    It works - you just need to check if(textField == self.blockedTextField) return NO; else return YES; – Grzegorz Krukowski Sep 10 '13 at 22:26
  • Really strange - I used it so often - can you paste your code with that approach ? – Grzegorz Krukowski Sep 10 '13 at 22:32
  • - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if(textField == birthdayTextField){ birthdayDatePicker.hidden = NO; birthdayDatePicker.alpha = 0; return NO; } else{ return YES; } } – Josue Espinosa Sep 10 '13 at 22:33
  • Are you sure that birthdayTextField is the right one ? Is it coming from storyboard or you manually assign value ? you can put a break point there and check if there is correct value under it. I'm quite positive it's the right approach. – Grzegorz Krukowski Sep 10 '13 at 22:39
  • A) I am sure B) The text field is nil, it gets set with the value the date picker returns – Josue Espinosa Sep 10 '13 at 23:07
  • 2
    Josue, you have to make sure you set the delegate of your UITextField to the UIViewController with the textFieldShouldBeginEditing method. – Elijah Feb 11 '15 at 06:32
  • The system calls this delegate method when the UITextField becomes first responder. That can happen for other reasons besides a user tap. – jk7 Mar 13 '23 at 18:48
24

Maybe, you can add UITapGestureRecognizer in the superview, detect if the touch is inside the frame, and then do something.

Detect touch if it is inside the frame of the super view

  1. Create UITapGestureRecognizer and add that to the UITextField's super view.
  2. Implement the target selector and check if the gesture's state has ended.
  3. Call your method.

Objective-C

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
[self.textField.superview addGestureRecognizer:tapGesture];


- (void) didRecognizeTapGesture:(UITapGestureRecognizer*) gesture {
    CGPoint point = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateEnded) {
        if (CGRectContainsPoint(self.textField.frame, point)) {
            [self doSomething];
        }
    }
}

Swift 3

func viewDidLoad() {
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))

    textField.superView?.addGestureRecognizer(tapGesture)
}

private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
    let point = gesture.location(in: gesture.view)

    guard gesture.state == .ended, textField.frame.contains(point) else { return }

    //doSomething()
}
dsapalo
  • 1,819
  • 2
  • 19
  • 37
Camo
  • 1,778
  • 14
  • 12