Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?
Use this.
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 180; distace of keyboard up.
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
If you are not using Interface builder, you can manually hook up events as explained here: How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?
If you are using Interface builder, then you don't need to create a background button.
You could do this:
You need to create a custom button at the background and then connect it to the IBAction
method that calls resignFirstResponder
on the your UITextField
something like this
Edit:
as you want to add the button programmatically than you can use this code
- (void)viewDidLoad
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
button.buttonType = UIButtonTypeCustom;
[button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
and than add your controller code after this.
use tochesBegin method for this purpose
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourFirstTextField resignFirstResponder];
[yourSecondTextField resignFirstResponder];
}
You do not need any thing else.When you touch anywhere on view then both textField resign no need to to know which one having focus.and when you touch textField then textField open keyboard by own.