简体   繁体   English

使用UITextField了解resignFirstResponder

[英]Understanding resignFirstResponder with UITextField

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. 编辑:tGilani的答案是最直接的方式,就像一个魅力,没有改变到UIControl。 But I guess jonkroll's answer also works. 但我想jonkroll的答案也有效。

try 尝试

[self.view endEditing:YES];

Update: 更新:

Take a boolean value and set it to false in init method. 取一个布尔值并在init方法中将其设置为false。 In your textFieldShouldReturn delegate method method, execute the code if it is false, skip otherwise textFieldShouldReturn委托方法方法中,如果为false则执行代码,否则跳过

- (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方法的方法中,将boolean设置为true。

boolean = YES;      
[self.view endEditing:YES];

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 . 接下来,在ViewController (或nib ,如果您没有使用故事板)的故事板场景中,将ViewController的view属性的类从UIView更改为UIControl The view property is effectively the background behind your other UI elements. view属性实际上是其他UI元素背后的背景。 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. 需要更改类类型,因为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:方法中,告诉视图控制器在视图收到UIControlEventTouchDown事件时执行dismissKeyboard方法。

- (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. 您关注的部分原因似乎是当键盘被解除时调用textFieldDidEndEditing: That is unavoidable, it will always be called whenever a text field loses focus (ie 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: . 听起来你的问题是,当用户点击textFieldDidEndEditing:的返回按钮时,你已经放置了代码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. 如果您不希望在用户触摸文本字段之外时运行该代码,那么这不是放置它的正确位置。

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 . 然后在文本字段发送控制事件UIControlEventEditingDidEndOnExit时通过Target-Action调用该方法。

[mainTextController addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];

Note that UIControlEventEditingDidEndOnExit is different than UIControlEventEditingDidEnd . 请注意, UIControlEventEditingDidEndOnExitUIControlEventEditingDidEnd不同。 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. 当用户触摸控件外部时编辑结束时调用前者,当用户按下返回键时编辑结束时调用后者。

You need to change your ViewController's view property from UIView to UIControl using the Identity Inspector: 您需要使用Identity Inspector将ViewController's view属性从UIView更改为UIControl

From there, you simply create an IBAction and tell the textfield to dismiss (which I am assuming is your mainTextController ). 从那里,你只需创建一个IBAction并告诉文本字段解散(我假设是你的mainTextController )。 If mainTextController is not the textfield you want the keyboard to dismiss on then change the resignFirstReaponder method to your textfield like so. 如果mainTextController不是你希望键盘解除的resignFirstReaponder ,那么将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". 然后从那里返回View Contoller的.xib文件并将操作连接到Control View并选择“Touch Down”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM