在viewDidLoad中添加手势识别器,handleBackgroundTap:使文本字段取消第一响应者状态.
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)]; tapRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapRecognizer]; } - (void) handleBackgroundTap:(UITapGestureRecognizer*)sender { [nameField resignFirstResponder]; [SexField resignFirstResponder]; }