簡體   English   中英

Xcode 4.2 按下“完成/返回”按鈕時如何隱藏文本框鍵盤

[英]How to hide Textbox Keyboard when “Done / Return” Button is pressed Xcode 4.2

我在 Interface Builder 中創建了一個文本字段。 我將它的“返回鍵”設置為完成。 這是一個只有一行的輸入(所以它不需要多行)。

當用戶點擊完成按鈕時如何隱藏虛擬鍵盤?

實現委托方法UITextFieldDelegate ,然后:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.yourIBtextField.delegate = self;
}

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

UITextView 沒有任何方法可以在用戶點擊返回鍵時調用。

即使這樣,如果您想這樣做,請實現 UITextViewDelegate 的 textView:shouldChangeTextInRange:replacementText: 方法,並在該方法中檢查替換文本是否為 \\n,隱藏鍵盤。

可能還有其他方法,但我不知道任何方法。

確保聲明對 UITextViewDelegate 協議的支持。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

使用 next 方法在 UIViewController 上創建類別

- (void)hideKeyboard
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                               to:nil
                                             from:nil
                                         forEvent:nil];
}

暫無
暫無

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

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