Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problems making keyboard vanish (resignFirstResponder)

I was following the Apple Developer Connection's instructions on creating an iPhone app (the with the text field, label, and button, where clicking the button makes the label display the entry in the text field).

I can't get the keyboard to disappear at all, not when tapping outside of the text field, not when tapping the button, and not when tapping "Return/Go/etc." on the bottom right of the keyboard.

<pre>
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField) {
[textField resignFirstResponder];
}
return YES;
}
</pre>

In Interface Builder, when I look at the text field attributes for that text field, under "Text Input Traits", I have "Return Key" as default. I would like either one of two behaviors - either make the button perform the same function as the button, and vanish afterward; or have a button that makes the keyboard simply vanish.

Any ideas what I might be doing incorrectly?

Macbook Pro (silver), Mac OS X (10.5.7)

Posted on Aug 5, 2009 11:10 PM

Reply
Question marked as Best reply

Posted on Aug 6, 2009 12:04 AM

make sure your function is getting called (you can use a NSLOG in the if statement). If it's not called then you should add the textfield delegate to first responder (in interface builder), or textField.delegate = self; on viewdidload (this in case your viewcontroller already implements uitextfielddelegate).
2 replies
Question marked as Best reply

Aug 6, 2009 12:04 AM in response to g-pod

make sure your function is getting called (you can use a NSLOG in the if statement). If it's not called then you should add the textfield delegate to first responder (in interface builder), or textField.delegate = self; on viewdidload (this in case your viewcontroller already implements uitextfielddelegate).

Aug 6, 2009 1:13 AM in response to g-pod

g-pod wrote:
I can't get the keyboard to disappear at all, not when tapping outside of the text field, not when tapping the button, and not when tapping "Return/Go/etc." on the bottom right of the keyboard.

It's quite likely that your textField instance var isn't connected to the text field object in IB. You can see what's going on by making the commented changes to textFieldShouldReturn:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
NSLog(@"textField=%@", textField); // <-- log value of textField instance var
// if (theTextField == textField) {// <-- temporarily comment out if condition
[textField resignFirstResponder];
//} // <--
return YES;
}

If the log in your Debugger Console says that textField is (null), open the controller xib file in IB and make sure the textField outlet of the view controller (File's Owner) is connected to the text field in the View editor window. One way to inspect and make the connection is to ctrl-click on the File's Owner icon to bring up the black connections panel. The textField outlet should show up in that panel. If it's not connected, ctrl-click on the connector circle and drag to the text field.

If you don't see a textField outlet in the File's Owner connection panel, make sure the textField instance var is declared as IBOutlet in your view controller class @interface file (e.g. MyViewController.h). Before rebuilding, select Clean All Targets in the Xcode Build menu.
<div class="jive-quote">In Interface Builder, when I look at the text field attributes for that text field, under "Text Input Traits", I have "Return Key" as default.
That selection only affects the label that appears on the keyboard's Return key.
make the button perform the same function as the button, and vanish afterward;

I assume you meant for one of these two buttons to be something else. Maybe the keyboard return key? Anyway if my guess about the textField connection was correct, you'll probably have what you wanted.
or have a button that makes the keyboard simply vanish.

As above, once the textField ivar is connected to the actual text field, the resignFirstResponder message will work whenever it's sent.

If my guess is wrong about the connection, please post the log message produced by the above NSLog() code. Also please post a link to the instructions you're following. There are more than one "Hello World" tutorials, so we'll need to be on the same page to do some more debugging.

- Ray

p.s.: When I started this message I hadn't seen alex.bbd's response. Alex makes a good point. textFieldShouldReturn won't be called unless the controller is designated as the text field's delegate. This point only applies to the action of the Return key, so you still need to check the connection I discussed above. After that however, if clicking outside the text field works, but the keyboard Return doesn't, go back to IB and make sure the text field's delegate outlet is connected to the File's Owner. - R

Problems making keyboard vanish (resignFirstResponder)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.