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

Dismissing the keyboard in the iPhone 101 Tutorial

Is anyone able to dismiss the keyboard in creating the app in the iPhone 101 tutorial? I have literally copied and pasted the code from the View Controller implementation and interface files, and the keyboard still doesn't go away when I click "Done." In fact, if I place a breakpoint in the textFieldShouldReturn method, the app doesn't stop and the debugger never breaks. It's like the UITextField never fires off textFieldShouldReturn. I've even tried to set the view as the delegate, as other threads have suggested:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
textField.delegate = self;

changeGreeting, though, works just fine. This is in the simulator using Beta 5, by the way. I haven't tried it on a device just yet.

Here's my controller's interface code:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *textField;
IBOutlet UILabel *label;
NSString *string;
}

@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, copy) NSString *string;
- (IBAction)changeGreeting:(id)sender;
@end

And here's my controller's implementation code:

#import "MyViewController.h"
@implementation MyViewController
@synthesize textField;
@synthesize label;
@synthesize string;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
textField.delegate = self;
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (IBAction)changeGreeting:(id)sender {
self.string = textField.text;
NSString *nameString = string;
if ([nameString length] == 0) {
nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!",
nameString];
label.text = greeting;
[greeting release];
}

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

Any help would be appreciated.

Santa Rosa MacBook, Harpertown Mac Pro, Mac OS X (10.5.2)

Posted on May 28, 2008 8:10 AM

Reply
15 replies

May 28, 2008 9:34 AM in response to Shu Chow

As the documentation says, you must have a target for the UIControlEventEditingEndOnExit action for the textFieldShouldReturn method to be called. (The docs are a little out of date, the action is actually called UIControlEventEditingDidEndOnExit). See UITextFieldDelegate Protocol reference for mor details.

May 30, 2008 3:31 PM in response to Shu Chow

Hi Shu,

I had the same problem as you until I figured you can simply set the delegate of the text box in this function:

- (void)viewDidLoad {
textField.delegate = self;
}

If you've done this the tutorial example works as a charm... just keep textFieldShouldReturn as it is.

I hope this helps you and I wish you good luck. 🙂

Jan 1, 2009 6:39 PM in response to Shu Chow

The delegate shouldn't need to be set programmatically. Instead, make sure you've made all the appropriate connections to the "ControllerView.xib" using Interface Builder as described in the "Making Connections" page of the tutorial:

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone 101/Articles/chapter6_section4.html

Particularly, make sure that the "textField" outlet of the File's Owner (which should be MyViewController) is connected to the UITextField you've placed in the view.

Jun 25, 2009 6:35 AM in response to akollegger

Thank you akollegger! I found that I needed to make sure you have all the appropriate connections. In the "Configuring the View" page ( http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone 101/Articles/05_ConfiguringView.html), I found that I had missed the following line in the "Making Connections" section:

Connect the label and textField outlets. Control click File’s Owner to display a translucent panel that shows all the available outlets and actions, then drag from the circle to the right of the list to the destination to make the connection.


The picture shows you how to connect the label, but the text tells you that you need to connect both the label and the textField outlets.

When I went back and looked at the outlets for MyViewController (double click on MyViewController.xib, Ctr-click on "File's Owner"), I noticed that I had an outlook connection from "label" to a Label and mousing over it highlighted the label in the View. It took a while to realize that I was missing the connection between "textField" and the UITextField in the View. Without that connection, there is no routing of the textFieldShouldReturn event from the keyboard.

To fix the problem, drag from the open circle next to "textField" to the UITextField in the View. Voila!

Hope this helps!

- Peter

Dismissing the keyboard in the iPhone 101 Tutorial

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