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