我有一个应用程序,其UITableView
使用自定义单元格,我为CustomCell
创建了一个新类。每按一个按钮就会创建一个新单元格,每个单元格都有一个UITextField
。我想知道如何在点击它后让单元格中的UITextField
重新响应第一响应者。请记住,它是在另一个名为CustomCell
的类中初始化的。
UIViewController
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard{
}
(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if (indexPath.row%2==0){
n=1;
cell.inOut.text = [NSString stringWithFormat:@"Entrada %d:", indexPath.row/2 +1];
cell.entryTime.text = [NSString stringWithFormat:@"%@", [_entryArray objectAtIndex:(indexPath.row+2)]];
if (indexPath.row==0){
cell.delta.text=@"";
}
else {
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
}
}
else{
cell.inOut.text = [NSString stringWithFormat:@"Saída %d:", (indexPath.row+1)/2];
cell.entryTime.text = [NSString stringWithFormat:@"%@",[_entryArray objectAtIndex:(indexPath.row+2)]];
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
if (whichButton==YES){}
else{
}
n=2;
}
if( [indexPath row] % 2)
[cell setBackgroundColor:[UIColor whiteColor]];
else
[cell setBackgroundColor:[UIColor lightGrayColor]];
return cell;
}
CustomCell.h
代码:
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UITextField *inOut;
@property (strong, nonatomic) IBOutlet UILabel *entryTime;
@property (strong, nonatomic) IBOutlet UILabel *delta;
- (void) dismissInOutKeyboard;
@end
CustomCell.m
代码:
@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_inOut = [[UITextField alloc]init];
_entryTime = [[UILabel alloc]init];
_delta = [[UILabel alloc]init];
[self.contentView addSubview:_entryTime];
[self.contentView addSubview:_inOut];
[self.contentView addSubview:_delta];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@synthesize inOut=_inOut, entryTime=_entryTime, delta=_delta;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
_inOut = [[UITextField alloc]init];
_inOut.textAlignment = UITextAlignmentLeft;
_entryTime = [[UILabel alloc]init];
_entryTime.textAlignment = UITextAlignmentLeft;
_delta = [[UILabel alloc]init];
_delta.textAlignment = UITextAlignmentLeft;
[self.contentView addSubview:_entryTime];
[self.contentView addSubview:_inOut];
[self.contentView addSubview:_delta];
}
return self;
}
@end
答案 0 :(得分:4)
只需拨打findAndResignFirstResponder
即可查看您拥有的最顶层视图,即UIViewController.view
声明:
@interface UIView (findAndResignFirstResponder)
- (void)findAndResignFirstResponder;
- (UIView *)findFirstResponder;
@end
定义:
@implementation UIView (findAndResignFirstResponder)
- (void)findAndResignFirstResponder {
[self.findFirstResponder resignFirstResponder];
}
- (UIView *)findFirstResponder {
for (UIView *subView in self.subviews) {
if (subView.isFirstResponder)
return subView;
UIView* firstResponder = subView.findFirstResponder;
if(firstResponder)
return firstResponder;
}
return nil;
}
答案 1 :(得分:3)
我使用的一个粗略但有效的实现是创建一个新的UITextField,将其设置为第一个响应者,然后立即删除它。执行此操作(在视图中)的基本方法是执行以下操作:
UITextField *cell = [UITextField new];
[self addSubview:cell];
[cell becomeFirstResponder];
[cell resignFirstResponder];
[cell removeFromSuperview];
cell = nil;
这将创建一个新单元格,将其添加到视图中以便可以选择它,使键盘的焦点远离当前选定的单元格,隐藏键盘,然后删除新单元格。
编辑:如果您要将其他文本字段设置为之后的第一个响应者,则此代码完全没必要。然后,您只需在新文本字段上调用becomeFirstResponder
,键盘焦点就会自动切换。