当文本框没有IBOutlet到视图控制器时,如何关闭iOS中的键盘?我的情况是具有动态原型单元格的UITableView。其中一个单元格包含UITextField,但我无法添加IBOutlet,因为不允许在重复内容中插入。当文本框没有IBOutlet时关闭键盘
那么我怎样才能达到解雇键盘时,文本框没有插座?
当文本框没有IBOutlet到视图控制器时,如何关闭iOS中的键盘?我的情况是具有动态原型单元格的UITableView。其中一个单元格包含UITextField,但我无法添加IBOutlet,因为不允许在重复内容中插入。当文本框没有IBOutlet时关闭键盘
那么我怎样才能达到解雇键盘时,文本框没有插座?
您可以使用此:
[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];
}
OR
你能做到这些还..
- (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];
}
我希望它能帮助..
我更喜欢touchesBegan。但为了使用它,我需要额外的东西吗?我需要UITaoGestureRecognizer吗? – cateof
添加任何一种方法在你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
}
试试这个: -
在斯威夫特: -
UIApplication.sharedApplication().sendAction(Selector("resignFirstResponder"), to: nil, from: nil, forEvent: nil)
在Objective-C: -
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
说明: - 来自:forEvent
我们可以通过调用sendAction发送给第一响应者的行动:以在UIApplication单例中,并将nil作为目标,在您的情况下,您的第一个响应者是您的文本字段。
我假设你已经创建了自定义动态原型单元格,其中包含UITextField。
无需在您添加了UITableView的ViewController类中使用UITextField的IBOutlet。
创建自定义TableViewCell类,然后在自定义TableViewCell类中创建UITextField的IBOutlet。
@interface SimpleTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextField *simpleTxtField;
@end
将TableViewCell类设置为自定义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;
}
那么的tableView:UITableView的数据源中的cellForRowAtIndexPath方法设置自定义TableViewCell的(SimpleTableViewCell)文本字段代表自我。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SimpleTableViewCell *cell = [self.simpleTableView dequeueReusableCellWithIdentifier:@"simpleCell" forIndexPath:indexPath];
cell.simpleTxtField.delegate = self;
return cell;
}
,只要你选择电池的文本字段一个键盘将显示,当你点击返回键文本字段委托方法将得到调用和键盘将得到解雇。 希望这会有所帮助。
我实际上发现这条线,搜索旧的SO帖子,但我不知道在哪里添加该行。我添加了一个IBAction来捕捉结束编辑事件,然后[self.view endEditing:YES]但代码从未到达过 – cateof
您的意思是你想要捕捉结束编辑事件?意味着如何进行最终编辑?也许添加一个手势作为@krushnsinh答案可以帮助你 – anhtu
当我需要在[view endEditing:]之后点击其他东西时会发生什么?每次我点击一行(didSelectIndex ..)segue不执行。 – cateof