Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

how can I dismiss keyboard when I will just touch anywhere except UITextField?

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?

Upvotes: 13

Views: 9151

Answers (10)

Faisal Memon
Faisal Memon

Reputation: 3454

Typically in this scenario you will have a scroll view holding the rest of the User Interface. If you assign the delegate of that scroll view to your view controller, and say that you implement the protocol UIScrollViewDelegate, then just adding the following method will do the job:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

Upvotes: 1

Nikunj
Nikunj

Reputation: 280

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];
}

Upvotes: 0

Madhup Singh Yadav
Madhup Singh Yadav

Reputation: 8114

The answer by Ishu is a good one. But I think you should also give a try to :

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

  [self.view endEditing:YES];
}

Upvotes: 10

mrcrowl
mrcrowl

Reputation: 1469

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:

  1. Select your view (probably called View)
  2. Change to the (i) tab in the property inspector
  3. Change the class from UIView to UIControl (this gives you access to the Touch Down event).
  4. Hook the Touch Down event to your backgroundTap method (Ctrl + drag).
  5. Don't forget to Save changes

Upvotes: 0

Robin
Robin

Reputation: 10011

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 enter image description here

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.

Upvotes: 1

Sabby
Sabby

Reputation: 2592

You need to define the action for your button click this way

[infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];


And implement the click method ,as for example i have provided the animation on button click method.
-(void)backButtonClicked
{

    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
    [self.navigationController popViewControllerAnimated:YES]; 
    [UIView commitAnimations];

}


Hope this will help you ,the action is without using IB.Its all done through coding

Upvotes: 0

Ishu
Ishu

Reputation: 12787

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.

Upvotes: 24

KingofBliss
KingofBliss

Reputation: 15115

Use delegate,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
 [textField resignFirstResponder];
 return YES;
}

You can also create a method in your controller

 -(IBAction)editingEnded:(id)sender{
    [sender resignFirstResponder]; 
}

and then in Connection Inspector in IB connect Event "Did End On Exit" to it.

Upvotes: 1

BojanG
BojanG

Reputation: 1922

Create an invisible button, put it behind the UITextField and send resignFirstResponder message to your text fields when the button is tapped.

Upvotes: 1

Dave Murdock
Dave Murdock

Reputation: 114

Keep a reference to the UITextField in your view controller and call [yourTextField resignFirstResponder]

Upvotes: -1

Related Questions