在iOS應(yīng)用開發(fā)中,有三類視圖對象會打開虛擬鍵盤,進行輸入操作,但如何關(guān)閉虛擬鍵盤,卻沒有提供自動化的方法。這個需要我們自己去實現(xiàn)。
首先輸入完成后按鍵盤上的done關(guān)閉鍵盤
首先在Interface Builder中選擇TextFields,然后在Text Field Attributes中找到Text Input Traits,選擇Return Key為done。OK
定義方法
代碼如下:
- (IBAction) textF
實現(xiàn)方法
代碼如下:
//按完Done鍵以后關(guān)閉鍵盤
- (IBAction) textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
然后找到事件Did End On Exit,與textFieldDoneEditing關(guān)聯(lián),OK。
如果是數(shù)字鍵盤,沒有done鍵怎么辦呢,我們通過觸摸背景關(guān)閉鍵盤
定義方法
代碼如下:
- (IBAction) backgroundTap:(id)sender; //通過觸摸背景關(guān)閉鍵盤
實現(xiàn)方法
代碼如下:
//通過觸摸背景關(guān)閉鍵盤
- (IBAction) backgroundTap:(id)sender
{
[nameFiled resignFirstResponder];
[numberField resignFirstResponder];
}
然后選擇背景的Touch Down事件,關(guān)聯(lián) backgroundTap,OK
這種方法有一點需要注意的是要把UIView改成UIControl.
新聞熱點
疑難解答
圖片精選