How do I dismiss the keyboard when the textfield loses focus with UITextFieldDelegate?

Posted on 2012-09-01
Last Modified: 2012-10-18
How do I dismiss the keyboard when the textfield loses focus with UITextFieldDelegate?
Question by:charmingduck
    LVL 14

    Expert Comment

    by:Hamidreza Vakilian

    Author Comment

    What's the xib here? and do I put the " IBOutlet UITextView *myTextView;" in the .h file? and do I need to import any file to use UITextFieldDelegate?

    Where the following outlet is properly connected in your xib

        IBOutlet UITextView *myTextView;
    Use this in the view controller:

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

        UITouch *touch = [[event allTouches] anyObject];
        if ([myTextView isFirstResponder] && [touch view] != myTextView) {
            [myTextView resignFirstResponder];
        [super touchesBegan:touches withEvent:event];
    A tap on the View Controller's View, outside of the Text View, will resign the first responder, closing the keyboard.
    LVL 14

    Accepted Solution

    The XIB is the file which represents the design of your so-called form in Interface Builder.

    Yes you should add this line to your .h file before @end:
    @property (retain, nonatomic) IBOutlet UITextView *myTextView;

    Open in new window

    Then in your .m file add this line immediately after @implementation line:
    @synthesize myTextView;

    Open in new window

    Also click on you xib file. In Interface Builder right-click on the UITextField. Drag from the circle to the right side of "New referencing outlet" on "File's Owner" yellow 3D cube on left hand and choose myTextView.

    For using UITextFieldDelegate you don't need to import anything but to prevent Xcode from showing warnings: On your .h file add this line at the end of the @interface line:

    Open in new window

    So you @interface line will look something like this:
    @interface myViewController : UIViewController <UITextFieldDelegate>

    Open in new window

    (I was wondering that a UITextView just shows some text and does not accept keyboard input so keyboard won't pop-up therefore nothing to dismiss; I think you should replace UITextView with UITextField in above snippets)

    I suggest you to watch the Stanford's iPhone Development course trainings in iTunes U. They are complete and free video trainings which covers almost everything you need to know as beginner.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

         Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON ( If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
    The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now