簡體   English   中英

使用鍵盤的“返回”(完成)按鈕調用方法

[英]call a method with the "Return" (Done) Button of the keyboard

有沒有人可以給我一個示例方法,該方法通過按鍵盤的返回按鈕調用並將文本視圖的文本(之前輸入的)保存在 nsuserdefaults 中?

多謝 :)

確保您的UITextField的返回鍵類型設置為UIReturnKeyGo (這是針對鍵盤上的圖像):

theTextField.returnKeyType = UIReturnKeyGo;

然后用這個方法做你想做的事:

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    // Tell the keyboard where to go on next / go button.
    if(textField == theTextField)
    {
        // do stuff
    }

    return YES;
}

要從文本字段中獲取文本,只需調用theTextField.text並根據需要保存!

迅捷版

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // Tell the keyboard where to go on next / go button.
    if textField == theTextField {
        // do stuff
    }

    return true
}

如果您將 UITextField 動態添加到 UITableCell,您還需要為其設置委托:

    self.textfield.delegate = self;

同樣在您需要添加的頭文件中:

    @interface YourController: UIViewController <UITextFieldDelegate>

暫無
暫無

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

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