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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex