tags:

views:

2238

answers:

3

Hi All,

I have a UIView with multiple text boxes. Now i have used delegates to change the responder from from text field to another. In this case my key board goes away when the user comes to last text field.

but now i want to hide my key board when the user touches UIView(touches any where on screen apart from text boxes). Can some one help me with this.

Thanks

+7  A: 

Use resignFirstResponder in touchesBegan, like so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.myTextField resignFirstResponder];
    }
}

You may need to call this on multiple text fields if you're not sure where the focus is currently located; I haven't tested that case.

Additionally, in Interface Builder, you'll need to enable the "User Interaction Enabled" checkbox for the view, or programatically use:

myView.userInteractionEnabled = YES;
PCheese
But where to call this resignFirstResponder. How to know the user touched the UIView???
nbojja
I've edited the post to include an example within touchesBegan.
PCheese
Hey..Thanks for your help..it worked well with UIView. But i have another scenario where i have text boxes in UITableView. in this case your code is not working. I am very new to iPhone development, so please......
nbojja
If you want to find out which control to call resignFirstResponder on, you can use isFirstResponder. So in my code i call "if ([myTextField isFirstResponder]) [myTextField resignFirstResponder]; else if...". One could probably be more systematic and iterate through the subviews, look for views/controls that are UIResponder and do the same check as above.
freshfunk
+2  A: 

According to Beginning iPhone Development, you draw a round rect button so that it covers your entire UI; the complete screen. Then go to the Layout menu and click Send to Back. Then in the inspector, change the button's type from round rect to Custom. Now add a touch up inside event to this button and attach it to a method which handles it. Within the body of this method, make sure you have the statements:

[myTextFieldOne resignFirstResponder];
[myTextFieldTwo resignFirstResponder];

Basically send the resignFirstResponder message to each of your text fields, or any field that can produce a keyboard.

I'm actually really new to the iPhone SDK. I don't know if this is the best method, but it works and it's what I learned from the aforementioned book. Hope it helps.

Jorge Israel Peña
+1  A: 

Thanks Blaenk, I was trying to work out how to do this and didn't realise I could put a button in the background, nice trick! Here's my contribution (new to this site and to Cocoa Touch so it may not be the most robust code ever, but it's working so far...):

I call this method from the touchUpInside event on my UIButton:

-(void)closeKeyboard:(id)sender {
    UIView *theFirstResponder = [self findFirstResponder];
    if (theFirstResponder) {
     [theFirstResponder resignFirstResponder];
    }
}

Where this loop finds the firstResponder:

- (UIView *)findFirstResponder {  
    UIView *firstResponderView = nil;
    for (UIView *view in [self entryFields]) {  
     if ([view isFirstResponder]) {  
            firstResponderView = view;
      break;
     }
    }   
    return firstResponderView;  
}

It is dependent on each of the UITextField controls in the view having a tag assigned to them (again, can do that in Interface Builder).

My two cents anyway, though i'd better give something back!

Paul J