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]; }
[tempTextField setDelegate:self];