如何在UIViewController中隐藏键盘上的返回buttonclick-> iphone
您好我发现一些像这样的问题,但他们谈论textView,我有ViewController,与scrollView其中有6个textfield和一个textView我想要的function,使键盘消失在完成/返回buttonclick.I实现functionresign第一响应,当我点击scrollView之外的时候会隐藏我的键盘,但是这不完全是我想要的,因为我喜欢使它在button点击时消失。
谢谢你的帮助
设置一个符合UITextFieldDelegate协议的类,并让你的文本字段的委托成为这个类的一个实例。 实施方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
如下:
- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }
嗨,我发现它,所以与textfields点是在viewdidload添加此行:
textFieldOne.returnKeyType = UIReturnKeyDone; textFieldCislo.delegate = self; textFieldTwo.returnKeyType = UIReturnKeyDone; textFieldCislo.delegate = self; ...
而这个实现方法:
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textFieldOne) { [textFieldOne resignFirstResponder]; } ... }
经过相当多的时间,find有意义的东西,这就是我放在一起,它像一个魅力。
。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
// // 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
U可以通过点击视图中的任何位置来使用此方法来隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; }