ios 如何在返回按钮单击-> iphone 时隐藏 UIViewController 中的键盘

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

how to hide keyboard in UIViewController on return button click->iphone

objective-cxcodeiosscrollview

提问by Csabi

Hi I find some questions like that but they talk about textView, I have ViewController, with scrollView where are 6 textfield and one textView I want a function which makes the keyboard disappear on on done/return button click.I implemented functions resign to first responder, which hide my keyboard when i click outside of scrollView, but that is not exactly i want, because i like to make it disappear on button click too.

嗨,我发现了一些类似的问题,但他们谈论的是 textView,我有 ViewController,带有 scrollView,其中有 6 个文本字段和一个 textView 我想要一个功能,使键盘在完成/返回按钮点击时消失。我实现了功能辞职给第一响应者,当我在 scrollView 外部单击时隐藏我的键盘,但这并不是我想要的,因为我也喜欢让它在单击按钮时消失。

THanks for any help

谢谢你的帮助

回答by James Bedford

Set up a class that conforms to the UITextFieldDelegate protocol and make the delegate of your text fields an instance of this class. Implement the method:

设置一个符合 UITextFieldDelegate 协议的类,并使文本字段的委托成为此类的实例。实现方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField

As follows:

如下:

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

回答by Csabi

Hi i found it out so the point with textfields is to add this lines at viewdidload:

嗨,我发现了,所以文本字段的重点是在 viewdidload 中添加以下行:

textFieldOne.returnKeyType = UIReturnKeyDone;
    textFieldCislo.delegate = self;
textFieldTwo.returnKeyType = UIReturnKeyDone;
    textFieldCislo.delegate = self;
...

And this implement method :

这个实现方法:

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    if (theTextField == textFieldOne) {
        [textFieldOne resignFirstResponder];
    }
...
}

回答by user2904476

After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.

经过相当长的时间寻找有意义的东西后,这就是我放在一起的东西,它就像一个魅力。

.h

。H

//
//  ViewController.h
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;

@end

.m

.m

//
//  ViewController.m
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone;
    [_theTextField setDelegate:self];

}

// This part is more dynamic as it closes any text field when pressing return.  
// You might want to control every single text field separately but that isn't 
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}


@end

回答by Nithinbemitk

U can use this method to hide the keyboard by clicking any where in the view

您可以使用此方法通过单击视图中的任何位置来隐藏键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}