Skip navigation
This discussion is archived

Dismissing the keyboard in the iPhone 101 Tutorial

12327 Views 15 Replies Latest reply: Aug 3, 2009 7:28 PM by aarthi RSS
1 2 Previous Next
Shu Chow Level 1 Level 1 (0 points)
Currently Being Moderated
May 28, 2008 8:11 AM
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)
1 2 Previous Next


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.