隐藏键盘点击按钮?

时间:2022-09-06 21:08:42

I am creating iPhone app which I show below.

我正在创建iPhone应用程序,我在下面展示。

At the end of screen I have text field. I have added a delegate for the same. As it is number pad, I have added button seperately so that when button is clicked, they keyboard is hidden.

在屏幕的最后,我有文本框。我添加了相同的委托。因为它是数字键盘,所以我添加了一个按钮,这样当按钮被点击时,他们的键盘就会被隐藏起来。

Below is the code I have:

下面是我的代码:

.h

@interface SearchViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITextField *textField006;
@property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;

.m

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidBeginEditing");
    // Ensure the relevant text field is visible
    CGAffineTransform translation = CGAffineTransformIdentity;
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenHeight = screenSize.height;

    if (screenHeight==480 || screenHeight==568) {
            translation = CGAffineTransformMakeTranslation(0, -120);
        doneButton.hidden = NO;
        NSLog(@"line 3");
        [UIView beginAnimations:nil context:nil];
        self.view.transform = translation;
        [UIView commitAnimations];
    }
}

- (IBAction)doneAction:(id)sender {
    doneButton.hidden = NO;    
    doneButton.hidden = YES;
    [textField006 resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    self.view.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
    [self.textField006 resignFirstResponder];

}

Why isn't the keyboard hiding? How can I hide it?

为什么键盘不隐藏呢?我怎么能把它藏起来呢?

Keyboard == Decimal Pad Return key >> Go Auto-enable Return key = Ticked

键盘==十进制键盘返回键>> Go自动启用返回键=勾选

3 个解决方案

#1


18  

Be sure to use endEditing: if it's not currently hiding correctly.

一定要使用昵称:如果它当前没有正确隐藏。

About endEditing:

关于endEditing:

"endEditing causes the view (or one of its embedded text fields) to resign the first responder status."

“endEditing导致视图(或其中一个嵌入的文本字段)退出第一个responder状态。”

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."

该方法查看当前视图及其子视图层次结构,以获取当前的第一个responder文本字段。如果它找到了一个,它要求文本字段作为第一响应器退出。如果力参数设置为YES,则甚至不会询问文本字段;它*辞职。

So, the following should work (inside your button click action method):

因此,以下操作应该有效(在您的按钮中单击action方法):

[self.view endEditing:YES];

#2


0  

You can also make [self.textField006 resignFirstResponder]

你也可以做[自我]。textField006 resignFirstResponder]

#3


0  

Swift Version of @lifetimes Answer

@此生回答的Swift版本

self.view.endEditing(true)

it worked perfectly for me

这对我来说非常有效

#1


18  

Be sure to use endEditing: if it's not currently hiding correctly.

一定要使用昵称:如果它当前没有正确隐藏。

About endEditing:

关于endEditing:

"endEditing causes the view (or one of its embedded text fields) to resign the first responder status."

“endEditing导致视图(或其中一个嵌入的文本字段)退出第一个responder状态。”

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."

该方法查看当前视图及其子视图层次结构,以获取当前的第一个responder文本字段。如果它找到了一个,它要求文本字段作为第一响应器退出。如果力参数设置为YES,则甚至不会询问文本字段;它*辞职。

So, the following should work (inside your button click action method):

因此,以下操作应该有效(在您的按钮中单击action方法):

[self.view endEditing:YES];

#2


0  

You can also make [self.textField006 resignFirstResponder]

你也可以做[自我]。textField006 resignFirstResponder]

#3


0  

Swift Version of @lifetimes Answer

@此生回答的Swift版本

self.view.endEditing(true)

it worked perfectly for me

这对我来说非常有效