0

I know this is a very basic concept, but for some reason I'm having trouble getting the keyboard to dismiss when a user taps outside of the two UITextFields in my view controller. I've tried as many solutions on SO that I can find, and none are working, which tells me I'm misunderstanding them in some way. This is how I initialize the UITextFields :

// Min Price
    //UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 25, 70, 30)];

    tf.userInteractionEnabled = YES;
    tf.textColor = [UIColor blackColor];
    tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text= _minPrice;

    tf.textAlignment = NSTextAlignmentCenter;
    tf.layer.cornerRadius=8.0f;
    tf.layer.masksToBounds=YES;
    tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf.layer.borderWidth= 1.0f;

    // Max Price
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 25, 70, 30)];
    tf1.userInteractionEnabled = YES;
    tf1.textColor = [UIColor blackColor];
    tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text= _maxPrice;

    tf1.textAlignment = NSTextAlignmentCenter;
    tf1.layer.cornerRadius=8.0f;
    tf1.layer.masksToBounds=YES;
    tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf1.layer.borderWidth= 1.0f;

    //and so on adjust your view size according to your needs
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(70, 100, 200, 60)];

    [view addSubview:tf];
    [view addSubview:tf1];


    [self.view bringSubviewToFront: tf];
    [self.view bringSubviewToFront: tf1];

    [self.view addSubview:view];

and this is the function I'm using to dismiss the keyboard:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView * txt in self.view.subviews){
        if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
            [txt resignFirstResponder];
        }
    }
}
Ghobs
  • 839
  • 2
  • 14
  • 32
  • 1
    FYI you can replace the loop in `touchesBegan:` with `[self.view endEditing:TRUE];` – rebello95 Sep 27 '14 at 00:00
  • What class contains the `touchesBegan:withEvent:` method? – rmaddy Sep 27 '14 at 00:04
  • Did you try [this solution](http://stackoverflow.com/a/11276674/77567)? – rob mayoff Sep 27 '14 at 00:05
  • @rebello95 Nitpick - change `TRUE` with `YES`. Use `YES` and `NO` for `BOOL` types. – rmaddy Sep 27 '14 at 00:06
  • is the `touchesBegan:withEvent` function being called? I don't believe it will be called if your viewcontroller is a scrollview (tableview). `NSLog(@"hi mom");` at the top of your method and tap everywhere on screen and see if get any output. – TyloBedo Sep 27 '14 at 00:19

4 Answers4

0

You are only looking at the direct subviews of your view; your text fields are both another level down (subviews of a subview of your view). There are a number of ways to fix this. Easiest is to tag both views and just get them by tag; you could also keep a pointer to the active field (or both) and just dismiss it/them.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • I see. Would you mind showing me programmatically how that would be done in this instance? I get what you mean but don't know the exact syntax. – Ghobs Sep 27 '14 at 19:17
0

The easiest solution is to have an invisible uiview, enable touch interaction for it, and just detect when this view is touched to hide itself and also call the resignFirstResponder. Put the view bellow your text fields on the view hierarchy and it should work. You don't even need to know which textfield is active since all will hide.

Pochi
  • 13,391
  • 3
  • 64
  • 104
0

As others had said, the setup of my subviews turned out to be the problem, changed it to the code below and it now works.

    // Min Price
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 125, 70, 30)];
    [self.view addSubview:tf];

    tf.userInteractionEnabled = YES;
    tf.textColor = [UIColor blackColor];
    tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text= _minPrice;

    tf.textAlignment = NSTextAlignmentCenter;
    tf.layer.cornerRadius=8.0f;
    tf.layer.masksToBounds=YES;
    tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf.layer.borderWidth= 1.0f;

    // Max Price
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 125, 70, 30)];
    [self.view addSubview:tf1];

    tf1.userInteractionEnabled = YES;
    tf1.textColor = [UIColor blackColor];
    tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text= _maxPrice;

    tf1.textAlignment = NSTextAlignmentCenter;
    tf1.layer.cornerRadius=8.0f;
    tf1.layer.masksToBounds=YES;
    tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf1.layer.borderWidth= 1.0f;
Ghobs
  • 839
  • 2
  • 14
  • 32
-1
  1. fist click on the outside view you take button size to entire view and click on this button write this code.

    [self.view endEditing:YES];

Daxesh Nagar
  • 1,405
  • 14
  • 22