簡體   English   中英

從鍵盤附件查看按鈕ResignFirstResponder

[英]ResignFirstResponder from Keyboard AccessoryView Button

在我的視圖中,我有3個UITextField,分別為它們創建了一個用於鍵盤的AccessoriesView,如apple所解釋。

在我的附件視圖中,我添加了一個必須調用函數“ CloseKeyboard”的按鈕。 在此函數中,我無法找到一種方法來截獲UITextField,該UITextField在其中編寫鍵盤且需要在其上調用ResignFristResponder的方法。

為了在一個字段上添加accessoryView,我編寫了該代碼

if (field1.inputAccessoryView == nil) {
    field1.inputAccessoryView = [self createInputAccessoryView];   
}
if (field2.inputAccessoryView == nil) {
    field2.inputAccessoryView = [self createInputAccessoryView];   
}
if (field3.inputAccessoryView == nil) {
    field3.inputAccessoryView = [self createInputAccessoryView];   
}

這里是createInputAccessory函數:

-(UIView *)createInputAccessoryView {

    CGRect accessFrame = CGRectMake(0.0, 0.0, 20.0, 55.0);

    UIView *inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];

    UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    compButton.frame = CGRectMake(250.0, 10.0, 60.0, 40.0);

    [compButton setTitle: @"Close" forState:UIControlStateNormal];

    [compButton addTarget:self action:@selector(CloseKeyboard:)

                        forControlEvents:UIControlEventTouchUpInside];

    [inputAccessoryView addSubview:compButton];

return inputAccessoryView; }

我如何實現“ CloseKeyboard”功能,以便能夠從活動的UITextField中退出signFirstFirstResponder?

謝謝您的幫助!

從iOS 2.0及更高版本開始,有一種簡單的方法可以關閉鍵盤,而不必跟蹤當前活動的控件,或遍歷所有可用控件,或使用UITextFieldDelegate

[self.view endEditing:YES]

從文檔:

endEditing:

使視圖(或其嵌入式文本字段之一)退出第一響應者狀態。

-( BOOL )endEditing :( BOOL強制

參量

指定YES以強制第一響應者辭職,無論它是否願意這樣做。

返回值
如果視圖放棄了第一響應者狀態,則為YES否則,則為NO

討論區
此方法查看當前視圖及其子視圖層次結構中當前是第一響應者的文本字段。 如果找到一個,它將要求該文本字段辭去第一響應者的職務。 如果將force參數設置為YES ,則永遠不會詢問文本字段; 它被迫辭職。

作為第一個解決方案,我嘗試使用此實現,在這種情況下,每次field1成為firstResponder時我都強制執行,然后我辭職。

-(void) closeFunction:(id)sender{   
    [field1 becomeFirstResponder];
    [field1 resignFirstResponder];
}  

但這是一種解決方法...不是解決方案:P

暫無
暫無

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

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