簡體   English   中英

iOS 鍵盤不會關閉

[英]iOS Keyboard won't dismiss

我的視圖控制器上有一個帶有文本字段的表單。 當用戶觸摸文本字段以添加他們的狀態時,我會顯示一個 UIPicker 供他們選擇。 問題是,如果鍵盤是從以前的文本字段顯示的,我無法將其關閉。 這是我的代碼:

- (void) textFieldDidBeginEditing:(UITextField *)textField {

    if (self.currentTextField) {
        self.currentTextField = nil;
        self.currentTextField = (KIP_TextField*)textField;
    } else {
        self.currentTextField = (KIP_TextField*)textField;
    }


    if (self.currentTextField.tag == 0 || self.currentTextField.tag == self.arrSearchFields.count-1) {

        //kill the editing
        [self.view endEditing:YES];
        [self.currentTextField resignFirstResponder];

        //set the picker type
        if (self.currentTextField.tag == 0) {
            self.currentPickerType = SUPPLIER_PICKER;
        } else if (self.currentTextField.tag == self.arrSearchFields.count-1) {
            self.currentPickerType = STATE_PICKER;
        }

        if (!self.hasPicker) {
            [self addPicker];
        } else {
            [self closePicker:YES];
        }

    } else {
        [self closePicker:NO];
    }

}

對於那些回復 resignFirstResponder 的人:我有這個並且它有效,但是當我顯示選擇器時我需要關閉鍵盤。 因此,TF1 和 2 有 3 個文本字段,TF1、TF2、TF3,觸摸它們會啟動一個選擇器,當用戶選擇一個選擇器行時,文本字段將填充該值,然后選擇器被關閉。 但是,如果您先轉到 TF2,則會顯示鍵盤,您可以通過返回鍵或 texfieldDidEndEditing 將其關閉。 但是假設您沒有輸入任何文本但觸摸 TF1 或 3,現在顯示選擇器但鍵盤仍在屏幕上。 即使使用 self.view endEditing,我也無法解除它

嘗試關閉鍵盤時,對 UITextView 使用以下委托方法:

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    [textField resignFirstResponder];
    return NO;
}

確保添加委托

emailTextField.delegate = self

您應該在實現選擇器之前實現它。

您可以使用 textFieldShouldReturn 委托方法:

- (BOOL)textFieldShouldReturn:(UITextField*)textField 
{
    [textField resignFirstResponder];
    return NO;
}

或者

如果您有多個文本字段並且不知道哪個是第一響應者(或者您根本無法從編寫此代碼的任何地方訪問文本字段),您可以調用 endEditing: 在包含文本字段的父視圖上.

[[self view] endEditing:YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM