UITextView在iPhone中隐藏键盘 [英] UITextView hide keyboard in iphone
问题描述
当用户在iphone中的 UITextView
对象中按回车时,我想隐藏键盘。但是,神秘地说这不适用于 UITextView
,但适用于 UITextField
。我无法弄明白为什么......
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...
这就是我所做的:
1)我创造了XCode4中的基于视图的应用程序。
1) I created a view based application in XCode4.
2).xib创建 UITextView
, UITextField
和 UIButton
对象
2) in .xib created UITextView
, UITextField
and UIButton
objects
3)标记为 UITextField
和 UITextView
委托给出口处的文件所有者
3) Marked both UITextField
and UITextView
delegates to File's Owner in Outlets
4)在.h
5中添加< UITextFieldDelegate>
到@interface UIViewController在.m中添加 textFieldShouldReturn
函数
5) Added textFieldShouldReturn
function in .m
以下是代码:
.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文件
- (BOOL) textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"textFieldShouldReturn Fired :)");
[textBoxLarge resignFirstResponder];
[textBoxLittle resignFirstResponder];
return YES;
}
令人惊讶的是,在textBoxLittle(UITextField)的情况下,键盘正在消失但不在textBoxLarge的情况(UITextView)
Amazingly, the keyboard is disappearing in case of textBoxLittle (UITextField) but not in case of textBoxLarge(UITextView)
作为进一步检查我,按钮调用函数doSomething
As a further check I, made the button to call function doSomething
- (IBAction)doSomething:(id)sender {
[textBoxLarge resignFirstResponder];
[textBoxLittle resignFirstResponder];
}
当我按下按钮时,键盘在两个文本框中都消失了。
When I am pressing the button, keyboard is disappearing in both textboxes.
它让我疯狂为什么textFieldShouldReturn适用于小文本框,但不适用于大文本框。
Its driving me nuts why textFieldShouldReturn is working for small textbox, but NOT for large textbox.
请帮助!
推荐答案
您需要在 UITextViewDelegate
中编写代码并将其分配给您的班级。
You need to write code in UITextViewDelegate
and assign it to your class.
这篇关于UITextView在iPhone中隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!