How to hide number pad using background Tap?
سؤال
I study iOS SDK using this book and in 4 Chapter, when it tells you how to hide number pad with background tap I have a problem. I do that author says but nothing happened. How I can do this? What book's author says:
1)Create new method backgroundTap in ViewController.h
- (IBAction)touchBackground:(id)sender;
2)Add method in ViewController.m
-(void)touchBackground:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];}
3)Change class identity of View object in Interface Builder(as I understand in Xcode 4 its Control) from UIView to UIControl
4)Connect TouchDown method in events list with File's Owner and check method backgroundTap.
This is my code samples
P.S. I'm sorry for my English, I translated all information from the book to english because I have russian translate.
المحلول
I think you might make this simpler by just implementing a method like this:
- (IBAction)backgroundTouched:(id)sender {
[self.view endEditing:YES];
}
make the background view UIControl
instead of UIView
in interface builder and hook the touches up event to this method.
Have a look at this small example project. I have done exactly this in the secondViewController
where touching the background dismissed the keyboard. You can also see how I have changed the background to a UIControl
in secondViewController.xib
نصائح أخرى
Something you might want to try, without changing the background view, is:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
You may have done Step 4 ("Connect TouchDown method in events list with File's Owner and check method backgroundTap") incorrectly. Include an NSLog in your touchBackground method to check whether the method is being called.
-(void)touchBackground:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
NSLog(@"touchBackground was called");
}
If the message ("touchBackground was called") doesn't show up in your console, then you known touchBackground is not being called.