0

I have a simple custom table view cell that has a label and a textfield. Looks like this in the storyboard:

enter image description here

I would like to show the keyboard when the user clicks anywhere in the cell, including if they click the label. I was 99% sure the way to achieve this would be to call becomeFirstResponder when the cell is clicked.

Here is my simple ViewController:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.tableView setEstimatedRowHeight:44.0f];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custom"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    BOOL firstResponder = [cell becomeFirstResponder];

}

And my custom table view cell:

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

- (BOOL) becomeFirstResponder {
    return [self.textField becomeFirstResponder];
}

@end

I verified that becomeFirstResponder is called, however that is returning false. What am I missing?

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

2 Answers2

1

Think as @alex-i points out in a comment here:

This [text field not becoming first responder] can also occur when the textfield is briefly removed from the view/window hierarchy while becoming the first responder (e.g. reloading a UITableView or UICollectionView that holds that textfield).

Which will happen on selection.

Rather than use didSelectRowAtIndexPath, you can add a UITapGestureRecognizer to your tableView with an action like:

- (IBAction)tap:(UITapGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    BOOL firstResponder = [cell becomeFirstResponder];
}

And it will become first responder.

Community
  • 1
  • 1
beyowulf
  • 15,101
  • 2
  • 34
  • 40
0

If someone needs a swift alternative: You can connect outer UIViews with outlet collection. On each UIViewController class you have once call below. So that when the outer box is touched, editfield will be activated with keyboard. You can apply this to your labels as well. (By disabling label's User Interaction)

@IBOutlet var outerBoxes:[UIView]!

override func viewDidLoad() {
    super.viewDidLoad();
    for outerBox in outerBoxes { outerBox.respondForEditBox() };
...

But once you have to have this code:

extension UIView {
    func respondForEditBox() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIView.focusOnEditBox));
        self.addGestureRecognizer(tap);
    }

    func focusOnEditBox() {
        for sview in self.subviews {
            if sview is UITextField {
                sview.becomeFirstResponder();
            }
        }
    }
}

Inspired by @Esqarrouth's answer at Close iOS Keyboard by touching anywhere using Swift

Community
  • 1
  • 1
JSBach
  • 447
  • 1
  • 6
  • 13