繁体   English   中英

当文本字段没有IBOutlet时关闭键盘

[英]dismiss keyboard when the textfield does not have IBOutlet

当文本字段的视图控制器没有IBOutlet时,如何在iOS中关闭键盘? 我的案例是带有动态原型单元的UITableView。 这些单元格之一包含一个UITextField,但是我不能添加IBOutlet,因为在重复内容时不允许使用插座。

那么,当文本字段没有插座时,如何实现关闭键盘的功能呢?

在您的ViewController.m文件中添加任何一种方法:

选择-1

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

选择-2

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignFieds)];
[self.view addGestureRecognizer:tap];

- (void)resignFieds {

   //choice - 1 , check the all subviews and resign textfield
    for (UIView * txt in self.view.subviews){
    if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
        [txt resignFirstResponder];
    }
   else
  {
  [self.view endEditing:YES];
  }

}
  //choice 2 , no need to check any subviews ,
   [self.view endEditing:YES];

 Note : use any one choice 

}

您可以使用此:

[view endEditing:YES];

试试这些。

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taped:)];
[tap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap];


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

要么

你也可以做这些

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   UITouch *touch = [[event allTouches] anyObject];
   if (![[touch view] isKindOfClass:[UITextField class]]) 
   {
       [self.view endEditing:YES];
   }
   [super touchesBegan:touches withEvent:event];
}

我希望这会有所帮助。

尝试这个 :-

在Swift中:

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

在Objective-C中:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

说明:-

我们可以通过在UIApplication单例上调用sendAction:to:from:forEvent并向目标对象传递nil来向第一响应者发送操作,在这种情况下,您的第一响应者就是您的文本字段。

我假设您已经创建了具有UITextField的自定义动态原型单元。

在添加UITableView的ViewController类中,不需要UITextField的IBOutlet。

创建自定义TableViewCell类,然后在自定义TableViewCell类中创建UITextField的IBOutlet。

@interface SimpleTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UITextField *simpleTxtField;

@end

将TableViewCell类设置为Custom TableViewCell类,说“ SimpleTableViewCell”。 通过选择TableViewCell并在Xcode的右侧窗格中选择Identity Inspector图标。

在ViewController类中,确认为UITextField Delegate。 像这样。

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *simpleTableView;

@end

实现UITextField委托方法。

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

    [textField resignFirstResponder];
    return YES;
}

然后在UITableView数据源的tableView:cellForRowAtIndexPath方法中,将自定义TableViewCell的(SimpleTableViewCell)TextField委托设置为self。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SimpleTableViewCell *cell = [self.simpleTableView dequeueReusableCellWithIdentifier:@"simpleCell" forIndexPath:indexPath];

    cell.simpleTxtField.delegate = self;

    return cell;
}

每当您选择“单元格的文本字段”时,就会显示一个键盘,当您单击返回键时,将调用TextField委托方法,而键盘将被关闭。 希望这会有所帮助。

暂无
暂无

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

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