ios UITextView 在 iphone 中隐藏键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6680693/
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
UITextView hide keyboard in iphone
提问by jerrymouse
I want to hide keyboard when a user presses return in UITextView
object in iphone. However, mysteriously this is not working for UITextView
but working for UITextField
. I am unable to figure out why...
我想在用户按下UITextView
iphone 对象中的返回键时隐藏键盘。然而,奇怪的是,这不是UITextView
为UITextField
. 我无法弄清楚为什么...
This is what I did:
这就是我所做的:
1) I created a view based application in XCode4.
1)我在 XCode4 中创建了一个基于视图的应用程序。
2) in .xib created UITextView
, UITextField
and UIButton
objects
2) 在 .xib 中创建UITextView
,UITextField
和UIButton
对象
3) Marked both UITextField
and UITextView
delegates to File's Owner in Outlets
3)标记都UITextField
与UITextView
奥特莱斯代表以文件的所有者
4) Added <UITextFieldDelegate>
to @interface UIViewController in .h
4) 添加<UITextFieldDelegate>
到@interface UIViewController in .h
5) Added textFieldShouldReturn
function in .m
5)textFieldShouldReturn
在.m 中添加功能
Here are the codes:
以下是代码:
.h file
.h 文件
@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
UITextView *textBoxLarge;
UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;
- (IBAction)doSomething:(id)sender;
@end
.m file
.m 文件
- (BOOL) textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"textFieldShouldReturn Fired :)");
[textBoxLarge resignFirstResponder];
[textBoxLittle resignFirstResponder];
return YES;
}
Amazingly, the keyboard is disappearing in case of textBoxLittle (UITextField) but not in case of textBoxLarge(UITextView)
令人惊讶的是,键盘在 textBoxLittle (UITextField) 的情况下消失,但在 textBoxLarge(UITextView) 的情况下不会消失
As a further check I, made the button to call function doSomething
作为进一步检查,我制作了调用函数 doSomething 的按钮
- (IBAction)doSomething:(id)sender {
[textBoxLarge resignFirstResponder];
[textBoxLittle resignFirstResponder];
}
When I am pressing the button, keyboard is disappearing in both textboxes.
当我按下按钮时,两个文本框中的键盘都消失了。
Its driving me nuts why textFieldShouldReturn is working for small textbox, but NOT for large textbox.
为什么 textFieldShouldReturn 适用于小文本框,但不适用于大文本框,这让我很抓狂。
Please Help!
请帮忙!
采纳答案by Abdurrashid Khatri
You need to write code in UITextViewDelegate
and assign it to your class.
您需要编写代码UITextViewDelegate
并将其分配给您的班级。
回答by Roger C S Wernersson
Three things:
三件事:
Make your view implement UITextViewDelegate.
使您的视图实现 UITextViewDelegate。
@interface keyboardDisappearViewController : UIViewController
<UITextFieldDelegate, UITextViewDelegate>
Add the following method:
添加以下方法:
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
}
return YES;
}
Set the file's owner as delegate for the UITextView in the interface builder.
在界面构建器中将文件的所有者设置为 UITextView 的委托。
(BTW: Solution copied from the comments to the previous answer, as it took me a while to extract. I though others could benefit from my experience.)
(顺便说一句:解决方案从评论复制到上一个答案,因为我花了一段时间来提取。我虽然其他人可以从我的经验中受益。)
回答by Ranga
Simple trick
Set delegate
for your text view and then
简单的技巧delegate
为您的文本视图设置然后
doSomething
{
}
action connect to ext view for control event didEndOnExit and tuchupinside
操作连接到分机视图 control event didEndOnExit and tuchupinside
// To dismiss key board when user clicks enter/return
// 当用户点击进入/返回时关闭键盘
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
回答by Nvork
Use this code
使用此代码
Inherit UITextViewDelegate protocol in your Viewcontroller add the text
在您的 Viewcontroller 中继承 UITextViewDelegate 协议添加文本
@interface YourViewController () <UITextViewDelegate>
In viewDidLoad set yourself as a delegate:
在 viewDidLoad 中将自己设置为委托:
yourUITextView.delegate = self;
Implement the delegate method below:
实现下面的委托方法:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return NO;
}
回答by J-Dizzle
Key Points
关键点
Someone needs to be listening for the Return Key press
- UITextFieldDelegate
- textFieldShouldReturn(textField : UITextField)
Someone needs to manually dismiss the keyboard
- resignFirstResponder()
有人需要听回车键按下
- UITextFieldDelegate
- textFieldShouldReturn(textField : UITextField)
有人需要手动关闭键盘
- 辞职第一响应者()
#1:Create a UITextFieldDelegate and assign it as the delegate to your UITextField -
#1:创建一个 UITextFieldDelegate 并将其作为委托分配给您的 UITextField -
exampleTextField.delegate = yourUITextFieldDelegate;
#2:Have 'yourUITextFieldDelegate' contain the following -
#2:让“yourUITextFieldDelegate”包含以下内容 -
func textFieldShouldReturn(textField : UITextField) -> Bool {
self.titleField.resignFirstResponder(); //Here's the key!!!!!
return true; //true just says 'default behavior'
}
回答by Sven Pfeiffer
If you came here looking for the Swift solution, like I did, here you are :)
如果您像我一样来到这里寻找 Swift 解决方案,那么您来了 :)
extension keyboardDisappearViewController : UITextViewDelegate {
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
}
return true
}}
回答by Aaron Leanage
You have coded in the .h file:
您已在 .h 文件中编码:
@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
UITextView *textBoxLarge;
UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;
- (IBAction)doSomething:(id)sender;
@end
It should be:
它应该是:
@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
UITextView *textBoxLarge;
UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextField *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;
- (IBAction)doSomething:(id)sender;
@end