1

我有一个应用程序,它有一个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
4

2 回答 2

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;
}
于 2013-03-25T04:33:36.037 回答
3

我使用的一个粗略但有效的实现是创建一个新的 UITextField,将其设置为第一响应者,然后立即将其删除。执行此操作(在视图内)的基本方法是执行以下操作:

UITextField *cell = [UITextField new];
[self addSubview:cell];

[cell becomeFirstResponder];
[cell resignFirstResponder];

[cell removeFromSuperview];
cell = nil;

这将创建一个新单元格,将其添加到视图中以便可以选择它,使其将键盘的焦点从当前选定的单元格上移开,隐藏键盘,然后删除新单元格。

编辑:如果您之后要将另一个文本字段设置为第一响应者,则此代码完全没有必要。然后,您只需调用becomeFirstResponder新的文本字段,键盘焦点就会自动转移。

于 2013-03-25T02:18:02.150 回答