1. 新建一个项目,命名为HideKeyboard。拖入两个Label,两个TextField
2.点击ViewController.h, 写入以下代码
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITextField *userField;
IBOutlet UITextField *passwordField;
}
@property (nonatomic,retain) IBOutlet UITextField *userField;
@property (nonatomic,retain) IBOutlet UITextField *passwordField;
- (IBAction)tapTheBlankAera:(id)sender;
- (IBAction)pressReturn:(id)sender;
@end
3.点击ViewController.m, 写入以下代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize userField;
@synthesize passwordField;
-(IBAction)tapTheBlankAera :(id)sender
{
//resignFirstResponder 会通接受对象失去第一响应者的状态
[userField resignFirstResponder];
[passwordField resignFirstResponder];
}
-(IBAction)pressReturn :(id)sender
{
[sender resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.然后将user的TextField指向 IBOutletUITextField *userField;
将password的TextField指向 IBOutletUITextField *passwordField;
5. 将View的Custom Class设置为UIControl, 右键点击View的空白处
将Touch Down指向- (IBAction)tapTheBlankAera:(id)sender;
6.右键Name 右侧的TextField, 将Did End On Exit选项指向 - (IBAction)pressReturn:(id)sender;
然后对着 Password右侧的TextField点击右键, 将Did End On Exit选项指向 - (IBAction)pressReturn:(id)sender;
7. 点击运行就可以了.