xcode resignFirstResponder 没有在 textFieldShouldReturn 上隐藏键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3124828/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:26:52  来源:igfitidea点击:

resignFirstResponder not hiding keyboard on textFieldShouldReturn

xcodeipadios4keyboard

提问by favo

I have a view with a UITextField which should hide the keyboard when return is pressed.

我有一个 UITextField 视图,它应该在按下返回时隐藏键盘。

My function is this:

我的功能是这样的:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
 if ( textField == userPassword ) {
  [textField resignFirstResponder];
 }
 return YES;
}

Normally the keyboard should be hidden but it stays on the screen. resignFirstResponder is correctly called. What am I missing?

通常键盘应该隐藏但它留在屏幕上。resignFirstResponder 被正确调用。我错过了什么?

回答by Brandon

I see you have the iPad tag on this. Do you happen to be presenting a modal view using UIModalPresentationFormSheet? If so, it looks like this is a limitation of the FormSheet modal presentation (either Apple is doing it intentionally for some reason, or it is a bug). See these other questions for more details:

我看到你在这上面有 iPad 标签。您是否碰巧使用 UIModalPresentationFormSheet 呈现模态视图?如果是这样,看起来这是 FormSheet 模式演示的限制(Apple 出于某种原因有意这样做,或者它是一个错误)。有关更多详细信息,请参阅这些其他问题:

Modal Dialog Does Not Dismiss Keyboard

模态对话框不会关闭键盘

Modal View Controller with keyboard on landscape iPad changes location when dismissed

关闭时,横向 iPad 上带有键盘的模态视图控制器会更改位置

回答by tony.stack

There is this helpful method which allows you to dismiss the keyboard when presenting the Modal Dialog:

有一种有用的方法可以让您在呈现模态对话框时关闭键盘:

 - (BOOL)disablesAutomaticKeyboardDismissal { return NO; }

This will override the default behavior of the modal dialog set by Apple and allow you dismiss the keyboard. It is in the UIViewController Class.

这将覆盖 Apple 设置的模态对话框的默认行为,并允许您关闭键盘。它在 UIViewController 类中。

I hope this helps someone!

我希望这可以帮助别人!

回答by Marcos Issler

If you are using the Interface Builder, look if your UITextField has the delegated linked with your class.

如果您使用的是 Interface Builder,请查看您的 UITextField 是否具有与您的类链接的委托。

-Select your UITextField and in your Connections look if exits one connection in Outlets->delegate. If not, conect with you File's Owner Class.

- 选择您的 UITextField 并在您的连接中查看是否在 Outlets->delegate 中退出一个连接。如果没有,请与您的文件所有者类联系。

This need to be linked with your File's Owner Class. This delegate tell where to search for a method. If your are overriding a method, you need to tell where the object will search for that.

这需要与您的文件所有者类相关联。这个委托告诉在哪里搜索方法。如果您要覆盖一个方法,则需要告诉对象将在哪里搜索该方法。

回答by gmeroz

This solution worked for me after none of the above did. after calling resignFirstResponder i added a modal view & removed it.

在以上都没有之后,这个解决方案对我有用。在调用 resignFirstResponder 后,我添加了一个模态视图并删除了它。



    [myTextField resignFirstResponder];
    UIViewController *dummyController = [[UIViewController alloc] init];
    UIView *dummy = [[UIView alloc] initWithFrame:CGRectMake(-1, -1,1,1)];
    [dummyController setView:dummy];
    [self presentModalViewController:dummyController animated:NO];
    [dummyController dismissModalViewControllerAnimated:NO];

回答by drynaski

To deal with the bug mentioned by Brandon, you can try closing and re-opening your modal view controller as long as you still have a reference to it.

为了解决 Brandon 提到的 bug,你可以尝试关闭并重新打开你的模态视图控制器,只要你还有对它的引用。

[textField resignFirstResponder];
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:yourModalViewControllerReference animated:NO];

(where "self" should be the controller you used to originally open the modal view controller)

(其中“self”应该是您最初打开模态视图控制器时使用的控制器)

回答by jmundie

I was having the same problem. I realized that after connecting the delegate to the File's Owner in Interface Builder, I neglected to save in Interface Builder. Once I saved, I recompiled and the keyboard disappears correctly when hitting return.

我遇到了同样的问题。我意识到在 Interface Builder 中将委托连接到 File's Owner 后,我忽略了在 Interface Builder 中的保存。保存后,我重新编译,按回车键时键盘正确消失。

回答by madusanka

xcode 4.5.1

Xcode 4.5.1

Simply click control then on the textfield drag and release on the .hfile

只需单击控件,然后在文本字段上拖动并释放.h文件

(control key+ drag)

控制键+拖动

then in the pop up menu select

然后在弹出菜单中选择

connection=acton;
name= any name;
type=id;
event=did end on exit;
arguments=sender;

then click connect button

然后点击连接按钮

回答by WrightsCS

Did you remember to implement the UITextFieldDelegateprotocol?

你记得实现UITextFieldDelegate协议吗?

回答by Mike Gledhill

I have read so many articles about this issue, where the onscreen keyboard refuses to hide when you call resignFirstResponder, but none of the suggestions worked for me.

我已经阅读了很多关于这个问题的文章,当你打电话时屏幕键盘拒绝隐藏resignFirstResponder,但没有一个建议对我有用。

I'm using XCode 5 (iOS 7) and have a iPhone screen containing a couple of controls which require the onscreen keyboard, but if the user clicks on the UIButton, then I want the keyboard to disappear.

我正在使用 XCode 5 (iOS 7) 并且有一个 iPhone 屏幕,其中包含几个需要屏幕键盘的控件,但是如果用户单击UIButton,那么我希望键盘消失。

enter image description here

在此处输入图片说明

I probably wasted one full day experimenting with resignFirstResponderand adding disablesAutomaticKeyboardDismissalfunctions to return NO, but nothing worked. Once the onscreen keyboard appeared, I could never get it to disappear again.

我可能浪费了一整天的时间来尝试resignFirstResponder和添加disablesAutomaticKeyboardDismissal函数来返回 NO,但没有任何效果。一旦屏幕键盘出现,我就再也无法让它消失了。

But then I had a small brainwave (as I only have a small brain).

但是后来我的脑电波很小(因为我的大脑很小)。

Now, when the user clicks on my UIButton, I simply disablethe UITextFieldand UITextViewcontrols.

现在,当我的用户点击UIButton,我干脆禁用UITextFieldUITextView控制。

- (IBAction)btnDate_Tapped:(id)sender {
    //  The user has clicked on the "Date" button.
    self.tbClientName.enabled = NO;
    self.tbComments.editable = NO;

And suddenly, the app finds it has no editable text fields needing an onscreen keyboard, and it neatly slides the keyboard out of sight.

突然,该应用程序发现它没有需要屏幕键盘的可编辑文本字段,它巧妙地将键盘滑出视线。

(Relieved sigh.)

(松了口气。)

My UIButtonactually makes a popup dialog appear. When the user dismisses the popup, I re-enable these two controls, so if the user taps in one of them, the keyboard will appear again.

我的UIButton实际上会出现一个弹出对话框。当用户关闭弹出窗口时,我重新启用这两个控件,因此如果用户点击其中一个,键盘将再次出现。

-(void)popoverControllerDidDismissPopover:(UIPopoverController *) popoverController {
    //  The user has closed our popup dialog.
    //  We need to make our UITextField and UITextView editable again.
    self.tbClientName.enabled = YES;
    self.tbComments.editable = YES;
    ... etc...
}

Simple, isn't it !

很简单吧!

And surprisingly, this workaround even works on UIViewControllerswhich appear in Modalstyle.

令人惊讶的是,这种解决方法甚至适用于UIViewControllers出现在Modal样式中的情况。

I hope this helps other XCode victims out there.

我希望这可以帮助其他 XCode 受害者。

回答by Josh O'Connor

Swift 3.0:

斯威夫特 3.0:

override var disablesAutomaticKeyboardDismissal: Bool {
    get{
        return false
    }
    set {
        self.disablesAutomaticKeyboardDismissal = false
    }
}