[英]How to change the keyboard Done button behavior?
好吧,所以我的目標是當在UITextField
引入一些東西后點擊鍵盤完成按鈕時,它應該觸發我的一個操作但不解除鍵盤。
現在,我通過“退出時結束”事件將UITextField
與我的動作連接起來,每當我完成鍵入時點擊“完成”按鈕,鍵盤就會消失。
如果您需要進一步澄清,請與我們聯系。
嘗試在代碼中設置textField.delegate = self
,以及符合UITextFieldDelegate
協議。 然后實現此方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Do something
}
在Swift中它將是:
//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}
您必須使用UITextField
的委托方法。 (設置TextField的委托以在Interface Builder或源代碼中更正viewController)
然后,轉到textField的viewcontroller的.h文件:
@interface correctViewController(type yours) :UIViewController <UITextFieldDelegate>
//now correctViewController "understands" our textfield.
轉到correctViewController.m文件:
//Create delegate method of textField
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder]; //to hide keyboard
return NO;
}
看起來像這樣。
看看UITextFieldDelegate
協議參考: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html
您可以使用此方法來控制鍵盤的行為:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
return NO;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.