如何以编程方式closures键盘iOS

我以编程方式创build了一个UITextField使UITextField成为viewController的一个属性。 我需要closures屏幕上的返回和触摸键盘。 我能够让屏幕触摸解散,但按返回不起作用。

我已经看过如何通过故事板,直接分配和初始化UITextField对象而不创build它作为属性。 可能做什么? 我是一个小白菜 – 对不起!

 .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong, atomic) UITextField *username; @end .m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)]; self.username.placeholder = @"Enter your username"; self.username.backgroundColor = [UIColor whiteColor]; self.username.borderStyle = UITextBorderStyleRoundedRect; if (self.username.placeholder != nil) { self.username.clearsOnBeginEditing = NO; } _username.delegate = self; [self.view addSubview:self.username]; [_username resignFirstResponder]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesBegan:withEvent:"); [self.view endEditing:YES]; [super touchesBegan:touches withEvent:event]; } @end 

简单的方法是将UITextField的委托连接到self( self.mytestField.delegate = self ),并使用[textField resignFirstResponder];closures方法textFieldShouldReturn的键盘[textField resignFirstResponder];

解除键盘的另一种方法如下:

 [self.view endEditing:YES]; 

[self.view endEditing:YES]; 您想在哪里解除键盘(button事件,触摸事件等)。

像这样添加一个UITextField的委托方法:

 @interface MyController : UIViewController <UITextFieldDelegate> 

并设置你的textField.delegate = self; 那么还要添加两个UITextField委托方法

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; } // It is important for you to hide the keyboard - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 

//通过触摸背景来隐藏键盘

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

在swift中简单地使用这个来closures键盘:

 UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil) 

Swift 3

 UIApplication.shared.sendAction(#selector(UIResponder.resign‌​FirstResponder), to: nil, from: nil, for: nil) 

尝试了解iOS视图层次结构中第一个响应者的概念。 当你的文本框变为活动(或第一响应者)时,当你触摸它(或通过它messasge becomeFirstResponder编程方式),它提出了键盘。 所以要删除你的文本字段作为第一个响应者,你应该把消息resignFirstResponder传递给它。

 [textField resignFirstResponder]; 

而要隐藏键盘的返回button,您应该实现其委托方法textFieldShouldReturn:并传递resignFirstResponder消息。

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

这是我在我的代码中使用的。 它就像一个魅力!

在yourviewcontroller.h中添加:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

现在在.m文件中,将其添加到您的ViewDidLoad函数中:

 - (void)viewDidLoad { [super viewDidLoad]; //Keyboard stuff tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; tapRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapRecognizer]; } 

另外,在.m文件中添加这个函数:

 - (void)handleSingleTap:(UITapGestureRecognizer *) sender { [self.view endEditing:YES]; } 

在App Delegate中,你可以写

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

使用这种方式,你不能写太多的代码。

键盘popup后要closures键盘,有两种情况

  1. 当UITextField 在UIScrollView中时

  2. 当UITextField 在UIScrollView之外

2.当UITextField在UIScrollView之外时,覆盖UIViewController子类中的方法

您还必须为所有UITextView添加委托

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 
  1. 滚动视图中,轻敲外部不会触发任何事件 ,因此在这种情况下,请使用轻触手势识别器 ,拖放滚动视图的UITapGesture为其创build一个IBAction

要创build一个IBAction,请按住Ctrl并单击UITapGesture并将其拖到viewcontroller的.h文件中。

在这里,我已经将tappedEvent命名为我的操作名称

 - (IBAction)tappedEvent:(id)sender { [self.view endEditing:YES]; } 

信息来自以下链接,请参考更多信息或联系我,如果你不了解的数据。

http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/

对于ViewController一组UITextView

Swift 3.0

 for view in view.subviews { if view is UITextField { view.resignFirstResponder() } } 

Objective-C的

 // hide keyboard before dismiss for (UIView *view in [self.view subviews]) { if ([view isKindOfClass:[UITextField class]]) { // no need to cast [view resignFirstResponder]; } } 

首先,您需要在.h文件中添加文本字段删除。 如果没有声明(BOOL)textFieldShouldReturn:(UITextField *)textField这个方法不被调用,所以首先添加委托,并写入键盘隐藏代码到该方法。

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

试试这个..

所以这就是我在触摸背景或返回之后做的解雇。 我不得不在viewDidLoad中添加delegate = self,然后在.m文件中添加委托方法。

 .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong, atomic) UITextField *username; @end .m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)]; self.username.placeholder = @"Enter your username"; self.username.backgroundColor = [UIColor whiteColor]; self.username.borderStyle = UITextBorderStyleRoundedRect; if (self.username.placeholder != nil) { self.username.clearsOnBeginEditing = NO; } self.username.delegate = self; [self.username resignFirstResponder]; [self.view addSubview:self.username]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end 

我知道这已被其他人回答,但我发现另一篇文章也没有背景事件 – tableview或scrollview。

http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/

由于标签只说iOS我会发布Swift 1.2和iOs 8.4的答案,添加这些在你的视图控制器swift类:

 // MARK: - Close keyboard when touching somewhere else override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { self.view.endEditing(true) } // MARK: - Close keyboard when return pressed func textFieldShouldReturn(textField: UITextField!) -> Bool { textField.resignFirstResponder() return true } // MARK: - 

另外不要忘记在类声明中添加UITextFieldDelegate,并将你的文本字段委托给自己(视图)。

添加委托:UITextFieldDelegate

 @interface ViewController : UIViewController <UITextFieldDelegate> 

然后添加这个委托方法

//这应该完美

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[UITextField class]]) { [obj resignFirstResponder]; } }]; } 

当你在屏幕上使用多个文本字段使用这种方法,你不需要每次都提到文本字段

 [textField1 resignFirstResponder]; [textField2 resignFirstResponder]; 

Swift 2:

这是什么做每件事情!

Donebuttonclosures键盘或Touch outSideNext转到下一个input。

首先在StoryBoard上更改TextFiled Return Key

 override func viewDidLoad() { txtBillIdentifier.delegate = self txtBillIdentifier.tag = 1 txtPayIdentifier.delegate = self txtPayIdentifier.tag = 2 let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture") self.view.addGestureRecognizer(tap) } func textFieldShouldReturn(textField: UITextField) -> Bool { if(textField.returnKeyType == UIReturnKeyType.Default) { if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField { next.becomeFirstResponder() return false } } textField.resignFirstResponder() return false } func onTouchGesture(){ self.view.endEditing(true) } 

如果你不知道当前的视图控制器或文本视图,你可以使用响应者链:

 UIApplication.shared.sendAction(#selector(UIView.endEditing(_:)), to:nil, from:nil, for:nil)