Skip to main content

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?



Source: Tips4all
Source: Tips4allSource: CCNA FINAL EXAM

Comments

  1. 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.

    ReplyDelete
  2. 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];
    }

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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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.

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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:


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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.