I know how to use the following method by Interface Builder.
-(void)dismissKeyboard
{
[testTextField resignFirstResponder];
}
In this way, when I touch the area outside the keyboard or tap the "return", the keyboard will dismiss.
But I don't know how to make it all by code. Please teach me , thanks.
Here's .h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
UITextField *testTextField;
}
@property (nonatomic,retain) UITextField *testTextField;
@end
here's .m
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize testTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *tempTextField = [[UITextField alloc] init];
self.testTextField = tempTextField;
testTextField.frame = CGRectMake(100, 150, 200, 30);
testTextField.placeholder = @"Test";
testTextField.backgroundColor = [UIColor whiteColor];
testTextField.textColor = [UIColor blackColor];
[self.view addSubview:testTextField];
}
What you lack is the UITextFieldDelegate
with it comes a lot of textfield methods that will be called for different reasons.
Check out the apple docs! http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html
You should resign your keyboard in the
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}