Wednesday, July 6, 2011

How to hide keyboard when you press the button

in your .H-file: you must add dismissKeyboard method

"
#import


@interface MyViewController : UIViewController {
UITextField *textField;
UILabel *label;
NSString *string;
}

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

@end

"

and in your .m-file:

"
#import "MyViewController.h"

@implementation MyViewController
@synthesize textField;
@synthesize label;
@synthesize string;
- (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];
}

-(IBAction)dismissKeyboard: (id)sender {
[sender resignFirstResponder];
}
"

then, connect in the IB's inspector for the textfield the 'did end on exit' option and connect it to the 'dismissKeyboard'.

happy coding...............

No comments:

Post a Comment