[英]Dismiss The Keyboard - Multiple UITextFields in iOS 7
在下面你會找到我的主視圖控制器的.h和.m文件。
我有3個問題。
1.)因為我有多個uitextfields,我是否必須使用自己的resignFirstResponder語句設置每個字段? 2.)我會用哪種方法做到這一點? 3.)我的語法是否適合辭職第一響應者?
如果我可以在用戶點擊字段時關閉鍵盤而不是點擊返回鍵,那將是非常好的!
我知道之前有人問過並回答過這個問題,但老實說,我仍然有點困惑。
我正在使用故事板,XCode 5和iOS 7。
=============================
.h文件
@interface ViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *danceDate;
@property (weak, nonatomic) IBOutlet UITextField *dancePlace;
@property (weak, nonatomic) IBOutlet UITextField *danceTerminal;
@property (weak, nonatomic) IBOutlet UITextField *danceGate;
.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveFromParse];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
// SET DELEGATE HERE
//
// if I uncomment 1 of these lines, i'll get an error.
//
// _dancePlace.delegate = self;
// dancePlace.delegate = self;
// dancePlace = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
[textField resignFirstResponder];
return YES;
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
return YES;
}
請嘗試以下方法:
[[self view] endEditing:YES]
重新啟動textField:所有textField.delegate都應設置為ViewController的對象。 然后實現下面的委托方法。
-(BOOL) textFieldShouldReturn: (UITextField *) textField {
[textField resignFirstResponder];
return YES;
}
要點擊View,請關閉鍵盤:向ViewController.view添加點按手勢,如下所示:
//declare a property to store your current responder
@property (nonatomic, assign) id currentResponder;
//in viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:singleTap];
[singleTap release];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnTap:
- (void)resignOnTap:(id)iSender {
[self.currentResponder resignFirstResponder];
}
// was missing ; after the call --> [self.currentResponder resignFirstResponder]
// also in textFieldDidEndEditing set self.currentResponder = nil;
-(BOOL) textFieldShouldReturn: (UITextField *) textField{
[textField resignFirstResponder];
return YES;
}
還連接您的UITextField
代理 。
這個答案適用於iOS 7和arc,
用戶觸摸返回時關閉鍵盤:在ViewController中添加以下操作
-(IBAction)textFieldReturn:(id)sender { [sender resignFirstResponder]; }
接下來,在main.storyboard中選擇textField並從連接檢查器控件+將“End End On Exit”事件拖到視圖控制器。
用戶觸摸背景時關閉鍵盤:在ViewController中實現以下方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if ([YOUR_TEXT_FIELD isFirstResponder] && [touch view] != YOUR_TEXT_FIELD) { [YOUR_TEXT_FIELD resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; }
這是我在代碼中使用的內容。 它工作得很好,比其他答案更有效。
在yourviewcontroller.h中添加:
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
現在在.m文件中,將其添加到ViewDidLoad函數中:
- (void)viewDidLoad {
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}
另外,在.m文件中添加此函數:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
當有問題的UITextField
調用委托方法- (BOOL)textFieldShouldReturn:(UITextField*)textField
它將自己作為參數傳遞。
因此,作為此方法的參數可用的特定textField
是您關心的特定textField
。 在此委托方法中,您可以將其稱為“textField”。
這意味着您應該使用Mirko Catalano建議在textField
上調用resignFirstResponder
而不是像您正在執行的單個屬性。
Mirko建議驗證代表確實被分配也是至關重要的。 您需要確保nib或storyboard中的所有UITextField都具有指向File的所有者的委托屬性。 否則委托消息將無處可及,並立即被忽略!
嘗試使用self.dancePlace.delegate = self;
而不是dancePlace.delegate = self;
設置UITextFieldDelegate。 那樣有用嗎?
在快速3:
//Dismiss keyboard , When touch outside
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.view.endEditing(true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.