?
Solved

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

Posted on 2012-09-01
3
Medium Priority
?
2,060 Views
Last Modified: 2012-10-18
How do I dismiss the keyboard when the textfield loses focus with UITextFieldDelegate?
0
Comment
Question by:charmingduck
  • 2
3 Comments
 

Author Comment

by:charmingduck
ID: 38358980
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.
0
 
LVL 15

Accepted Solution

by:
Hamidreza Vakilian earned 1200 total points
ID: 38358998
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:
<UITextFieldDelegate>

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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