Previous 1 2 Next 15 Replies Latest reply: Aug 3, 2009 7:28 PM by aarthi
Shu Chow Level 1 (0 points)
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;

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, %@!",
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)
  • ooo27 Level 1 (35 points)
    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.
  • Shu Chow Level 1 (0 points)
    Great, thanks. Where and how do I set up the action?
  • ooo27 Level 1 (35 points)

    -(void)onTextChange:(id)sender {
    // dummy func must exist for textFieldShouldReturn event to be called

    - (void) init {
    // ...

    [myTextfield addTarget:self action:@selector(onTextChange:) forControlEvents:UIControlEventEditingDidEndOnExit]

    // ...
  • Shu Chow Level 1 (0 points)
    Thanks for you help, but it's still not working. I've attached the "Editing Changed" event for the text field to your onTextChange:, but the keyboard is still there. I've submitted Document Feedback to Apple pointing out the UIControlEventEditingDidEndOnExit requirement.
  • honkstar Level 1 (10 points)
    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.
  • Shu Chow Level 1 (0 points)
    That did the trick! Thank you, honkstar!

    Just for learning, why didn't it work when I set the delegate in initWithNibName: bundle:?
  • Littleodie914 Level 1 (0 points)
    I think it didn't work setting the delegate in the init method because the UITextField is created in Interface Builder, and isn't available that early programmatically.

    In other words, the UITextField in Interface Builder is created after the class is instantiated, but before the view is loaded.
  • Ron2008 Level 1 (0 points)
    I saw that you were able to help Shu, I tried using the same code and it didn't work for me. Any ideas?
    Thank you for you kind help
  • Kylom Level 1 (0 points)
    I had the same problem. It was solved without changing or adding anything to the code supplied in the tutorial : I ticked the "*Auto-enable Return key*" box in the Inspector for the text field (in "Text Input Traits").

  • Kylom Level 1 (0 points)
    There is obviously more to it : I have now unticked the "Auto-enable Return key" and the app still works OK. The keyboard is dismissed as it must be...

    Sorry for the "non solution"
  • akollegger Level 1 (0 points)
    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: 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.
  • lionpeal Level 1 (0 points)
    Thank you for this. I had the same problem as the original poster and it's because I missed this step:

    Set the text field’s delegate to be the File’s Owner (the view controller) by Control-dragging from the text field to the File’s Owner and selecting delegate in the translucent panel that appears.
  • Copy Ninja Kakashi Level 1 (0 points)
    Yup, that did it, thanks lionpeal
  • PeterDP Level 1 (0 points)
    Thank you akollegger! I found that I needed to make sure you have all the appropriate connections. In the "Configuring the View" page ( 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
Previous 1 2 Next