ios 使用 UITextField 了解 resignFirstResponder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11924445/
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
Understanding resignFirstResponder with UITextField
提问by Lior Pollak
I'm trying to get rid of the keyboard when the user touch outside my UITextField, by using this method:
当用户触摸我的 UITextField 外部时,我试图通过使用以下方法摆脱键盘:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[mainTextController resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
However, this seems to call a method that is called after pressing the return button on the keyboard, but I just want the keyboard to vanish, not to press return for me. How can I accomplish that?
然而,这似乎需要一个被按下键盘上的回车键后调用的方法,但我只是想在键盘消失,不按回车键我。我怎样才能做到这一点?
Thanks!
谢谢!
EDIT: tGilani's answer is the most straight-forward way, works like a charm, without changing to UIControl. But I guess jonkroll's answer also works.
编辑:tGilani 的答案是最直接的方式,就像魅力一样,无需更改为 UIControl。但我想 jonkroll 的回答也有效。
回答by tGilani
try
尝试
[self.view endEditing:YES];
Update:
更新:
Take a boolean value and set it to false in init method. In your textFieldShouldReturn
delegate method method, execute the code if it is false, skip otherwise
取一个布尔值并在 init 方法中将其设置为 false。在您的textFieldShouldReturn
委托方法方法中,如果为假则执行代码,否则跳过
- (BOOL) textFieldShouldReturn:(UITextField*)textField
{
if (!boolean)
{
// YOur code logic here
}
boolean = false;
}
in your method where you call the endEditing method, set boolean to true.
在调用 endEditing 方法的方法中,将布尔值设置为 true。
boolean = YES;
[self.view endEditing:YES];
回答by jonkroll
Here's how I've handled this before. First create a method on your view controller that will dismiss the keyboard by resigning first responder status on your text field:
这是我以前处理过的方法。首先在您的视图控制器上创建一个方法,该方法将通过在您的文本字段上退出第一响应者状态来关闭键盘:
- (IBAction)dismissKeyboard:(id)sender
{
[mainTextController resignFirstResponder];
}
Next, in your storyboard scene for your ViewController
(or nib
, if you are not using storyboards) change the class of your ViewController's view
property from UIView
to UIControl
. The view
property is effectively the background behind your other UI elements. The class type needs to be changed because UIView
cannot respond to touch events, but UIControl
(which is a direct subclass of UIView
) can respond to them.
接下来,在您的故事板场景中ViewController
(或者nib
,如果您不使用故事板)将 ViewController 的view
属性类从UIView
更改为UIControl
。该view
属性实际上是其他 UI 元素背后的背景。类类型需要更改,因为UIView
无法响应触摸事件,但UIControl
(它是 的直接子类UIView
)可以响应它们。
Finally, in your ViewController's viewDidLoad:
method, tell your view controller to execute your dismissKeyboard
method when the view receives a UIControlEventTouchDown
event.
最后,在您的 ViewController 的viewDidLoad:
方法中,告诉您的视图控制器dismissKeyboard
在视图接收到UIControlEventTouchDown
事件时执行您的方法。
- (void)viewDidLoad
{
[super viewDidLoad];
UIControl *viewControl = (UIControl*)self.view;
[viewControl addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchDown];
}
EDIT:
编辑:
Part of your concern seems to be that textFieldDidEndEditing:
is called when the keyboard is dismissed. That is unavoidable, it will always be called whenever a text field loses focus (i.e. first responder status). It sounds like your problem is that you have put code to perform when the user clicks the return button in textFieldDidEndEditing:
. If you do not want that code to run when the user touches outside of the text field, that is not the proper place to put it.
您的部分担忧似乎textFieldDidEndEditing:
是在键盘关闭时调用。这是不可避免的,只要文本字段失去焦点(即第一响应者状态),它就会始终被调用。听起来您的问题是当用户单击textFieldDidEndEditing:
. 如果您不希望在用户触摸文本字段外部时运行该代码,那么这不是放置它的正确位置。
Instead, I would put that code in a separate method:
相反,我会将该代码放在一个单独的方法中:
- (IBAction)textFieldReturn:(id)sender
{
if ([mainTextController isFirstResponder]) {
[mainTextController resignFirstResponder];
// put code to run after return key pressed here...
}
}
}
and then call that method via Target-Action when your text field sends the control event UIControlEventEditingDidEndOnExit
.
然后在您的文本字段发送控制事件时通过 Target-Action 调用该方法UIControlEventEditingDidEndOnExit
。
[mainTextController addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
Note that UIControlEventEditingDidEndOnExit
is different than UIControlEventEditingDidEnd
. The former is called when editing ends by the user touching outside the control, the latter is called when editing ends by the user pressing the return key.
请注意,这UIControlEventEditingDidEndOnExit
不同于UIControlEventEditingDidEnd
. 前者在用户触摸控件外编辑结束时调用,后者在用户按下返回键编辑结束时调用。
回答by Zack
You need to change your ViewController's
view property from UIView
to UIControl
using the Identity Inspector:
您需要使用 Identity Inspector将ViewController's
视图属性从更改UIView
为UIControl
:
From there, you simply create an IBAction
and tell the textfield to dismiss (which I am assuming is your mainTextController
). If mainTextController
is not the textfield you want the keyboard to dismiss on then change the resignFirstReaponder
method to your textfield like so.
从那里,您只需创建一个IBAction
并告诉文本字段关闭(我假设是您的mainTextController
)。如果mainTextController
不是您希望键盘关闭的文本字段,则将resignFirstReaponder
方法更改为您的文本字段。
- (IBAction)backgroundTap:(id)sender {
[myTextField resignFirstResponder];
}
then from there go back into your View Contoller's .xib
file and connect the action to the Control View and select "Touch Down".
然后从那里返回到您的视图控制器的.xib
文件并将操作连接到控制视图并选择“Touch Down”。