3

I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.

IBOutlet UITextView *myTextView;

And the method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    if ([myTextView isFirstResponder] && [touch view] != myTextView) {
        [myTextView resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

What I don't understand is how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.

I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)

Community
  • 1
  • 1

6 Answers6

26

Objective C:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     [[self view] endEditing:YES];
}

Swift:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

This is the best way I have found and it is very simple.

lukas_o
  • 3,776
  • 4
  • 34
  • 50
DirectX
  • 922
  • 10
  • 14
6

how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.

Because you don't. You have to override this method on the view of which the text field is a subview.

4

What I do, is change the overall UIView class to UIControl. enter image description here

This gives you a touchDown event you can link up to a method to resignFirstResponder. enter image description here
The UIControl still gives you all the functionality of a UIView.

-(IBAction)backgroundTap:(id)sender
{
    [text1 resignFirstResponder];
    [text2 resignFirstResponder];
    [textLogin resignFirstResponder];
    [textPassword resignFirstResponder];
} // Resign all responders
Darren
  • 10,182
  • 20
  • 95
  • 162
0
- (void)viewDidLoad
{
     //for keybord hide when touch outside:


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(hidekeybord)];
    [self.view addGestureRecognizer:tap];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)hidekeybord
{

    [_yourtextfield.txt resignFirstResponder];

}
Paresh Hirpara
  • 487
  • 3
  • 10
0
In .h
@property (nonatomic, assign) id currentResponder;

In .h
//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:singleTap];

//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)iSender {
    [self.currentResponder resignFirstResponder];
}
ArNo
  • 2,278
  • 1
  • 22
  • 19
-2

Try the quick and dirty way:

Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.

Lost Sorcerer
  • 905
  • 2
  • 13
  • 26
  • 1
    Unnecessarily overcomplicated and hackish. OP's solution with a but of help and clean and that is the way it should be done. -1. –  Sep 01 '12 at 12:56