简体   繁体   English

隐藏键盘上的按钮单击?

[英]Hiding the Keyboard on button click?

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 。H

@interface SearchViewController : UIViewController<UITextFieldDelegate>

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

.m .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 键盘==小数键盘返回键>>转到自动启用返回键=勾选

Be sure to use endEditing: if it's not currently hiding correctly. 请确保使用endEditing:如果当前未正确隐藏。

About endEditing : 关于endEditing

"endEditing causes the view (or one of its embedded text fields) to resign the first responder status." “ endEditing使视图(或其嵌入的文本字段之一)放弃第一响应者状态。”

"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." “此方法查看当前作为第一响应者的文本字段的当前视图及其子视图层次结构。如果找到该方法,它将要求该文本字段放弃作为第一响应者。如果force参数设置为YES,则该文本甚至从来没有问过这个领域;它被迫辞职。”

So, the following should work (inside your button click action method): 因此,以下方法应该起作用(在您的按钮单击操作方法内部):

[self.view endEditing:YES];

您也可以使[self.textField006 resignFirstResponder]

Swift Version of @lifetimes Answer @lifetimes答案的Swift版本

self.view.endEditing(true)

it worked perfectly for me 对我来说效果很好

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

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