[textField resignFirstResponder]
#import "FormViewController.h" @interface FormViewController ()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *textFieldA; @property (weak, nonatomic) IBOutlet UITextField *textFieldB; @property (weak, nonatomic) IBOutlet UITextField *textFieldC; @property (weak, nonatomic) IBOutlet UITextField *textFieldD; @property (weak, nonatomic) IBOutlet UITextField *textFieldE; @property (weak, nonatomic) IBOutlet UITextField *textFieldF; @property (weak, nonatomic) IBOutlet UITextField *textFieldG; @property (weak, nonatomic) IBOutlet UITextField *textFieldH; @property (weak, nonatomic) IBOutlet UITextField *textFieldI; @property (weak , nonatomic) UITextField *editingTextField ; // 跟蹤當前處於編輯狀態的textfield @end @implementation FormViewController @synthesize textFieldA,textFieldB,textFieldC, textFieldD,textFieldE,textFieldF,textFieldG,textFieldH, textFieldI,editingTextField; - (void)viewDidLoad { [super viewDidLoad]; self.textFieldA.delegate = self ; self.textFieldB.delegate = self ; self.textFieldC.delegate = self ; self.textFieldD.delegate = self ; self.textFieldE.delegate = self ; self.textFieldF.delegate = self ; self.textFieldG.delegate = self ; self.textFieldH.delegate = self ; self.textFieldI.delegate = self ; } - (void)viewDidUnload { [self setTextFieldA:nil]; [self setTextFieldB:nil]; [self setTextFieldC:nil]; [self setTextFieldD:nil]; [self setTextFieldE:nil]; [self setTextFieldF:nil]; [self setTextFieldG:nil]; [self setTextFieldH:nil]; [self setTextFieldI:nil]; [self setEditingTextField:nil]; [super viewDidUnload]; } // UITextFieldDelegate 方法 - (void)textFieldDidBeginEditing:(UITextField *)textField { self.editingTextField = textField ; } // 提交Action - (IBAction)submitForm:(UIBarButtonItem *)sender { [self.editingTextField resignFirstResponder]; // 提交表單,彈出提示等待框,略 }
[self.editingTextField resignFirstResponder];
來關閉鍵盤。
流程上邏輯很簡單,但總感受只爲實現關閉鍵盤這個功能,須要添加 atom
self.textFieldA.delegate = self ; self.textFieldB.delegate = self ; self.textFieldC.delegate = self ; self.textFieldD.delegate = self ; self.textFieldE.delegate = self ; self.textFieldF.delegate = self ; self.textFieldG.delegate = self ; self.textFieldH.delegate = self ; self.textFieldI.delegate = self ;
- (BOOL)endEditing:(BOOL)force官網文檔釋義:
Causes the view (or one of its embedded text fields) to resign the first responder status.
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
註銷當前view(或它下屬嵌入的text fields)的first responder 狀態。
該方法會在當前view以及其subview層次結構中需找當前處於first responder狀態的text field。若是找到的話會註銷其first responder狀態,若是指定force參數爲YES,則再也不詢問text field,而直接強制註銷其first responder狀態。
[self.view endEditing:YES];鍵盤立馬關閉!
注:描述於IOS5.1下 spa