ios 如何让 UITextView“完成”按钮退出FirstResponder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make UITextView "Done" button resignFirstResponder?
提问by Jason
I am trying to make my editable UITextView
resign the keyboard (resignFirstResponder
) when the user taps "Done." Using a UITextField
, I have been able to do this with the following code:
当用户点击“完成”时,我试图让我的可编辑UITextView
退出键盘 ( resignFirstResponder
)。使用 a UITextField
,我已经能够使用以下代码来做到这一点:
- (IBAction)doneEditing:(id)sender {
[sender resignFirstResponder];
}
... and then to attach it to the relevant UITextField
in Interface Builder to the action "Did End on Exit."
...然后将其附加到UITextField
Interface Builder 中的相关操作“Did End on Exit”。
However, with a UITextView
, I can't seem to access the "Did End on Exit" action. Any suggestions on how to make this happen?
但是,使用UITextView
,我似乎无法访问“退出时确实结束”操作。关于如何做到这一点的任何建议?
采纳答案by Jordan
new Answer
新答案
On your View, you'd have a UIBarButton ("Done") that is connected to the IBAction below:
在您的视图上,您将有一个连接到下面 IBAction 的 UIBarButton(“完成”):
- (IBAction)doneEditing:(id)sender {
[textView resignFirstResponder];
}
Where textView is your textView outlet defined in your .h file and connected in Storyboard or .xib file. Like this:
其中 textView 是在 .h 文件中定义并在 Storyboard 或 .xib 文件中连接的 textView 插座。像这样:
@property (retain, nonatomic) IBOutlet UITextView *textView;
old Answer
旧答案
Check the following:
检查以下内容:
- Is UITextViewDelegate specified in .h
- Implement delegate method for uitextview: textViewShouldEndEditing, return YES
- make sure your .m (controller) is the delegate for uitextview in IB
- resignFirstResponder should now work.
- .h 中是否指定了 UITextViewDelegate
- 为 uitextview 实现委托方法:textViewShouldEndEditing,返回 YES
- 确保您的 .m(控制器)是 IB 中 uitextview 的委托
- resignFirstResponder 现在应该可以工作了。
回答by Rich Lowenberg
The accepted answer didn't work for me. Instead, the following delegate method should be invoked like this:
接受的答案对我不起作用。相反,应该像这样调用以下委托方法:
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder];
return NO;
}else{
return YES;
}
}
Paste that into the class that you assign to be the UITextView delegate and it'll work.
将其粘贴到您指定为 UITextView 委托的类中,它就会起作用。
回答by Axel Guilmin
You can implement UITextViewDelegate
and wait for "\n", in Swift 4 :
您可以UITextViewDelegate
在 Swift 4 中实现并等待 "\n" :
myTextView.delegate = self
// ...
extension MyViewController : UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
}
回答by Damien Praca
To have the done button dismiss the keyboard and resignFirstResponder
you have to implement a delegate method.
要让完成按钮关闭键盘,resignFirstResponder
您必须实现一个委托方法。
In your .h implement the UITextFieldDelegate
@interface YourViewController : UIViewController <UITextFieldDelegate>
Then implement the textFieldShouldReturn in your .m
-(BOOL) textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }
Don't forget to link the delegate of UITextField to the file's Owner (very important)
在你的 .h 中实现 UITextFieldDelegate
@interface YourViewController : UIViewController <UITextFieldDelegate>
然后在你的 .m 中实现 textFieldShouldReturn
-(BOOL) textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }
不要忘记将 UITextField 的委托链接到文件的所有者(非常重要)