Nimit Pattanasri
Nimit Pattanasri

Reputation: 1602

The second tap on UITextField (within a UITableViewCell subclass) unexpectedly dismisses the keyboard

I'm implementing a UITableView that holds a few custom UITableViewCells, each of which contains UITextField.

Unlike most people who want a keyboard to disappear upon some events, I want it to appear all the time. The issue is that the first tap on UITextField triggers a keyboard to appear without any problems. But once I tap on it again the keyboard that currently appears now disappears.

Moreover, another related issue is I cannot change the cursor's position inside UITextField. Once I hold the tap on UITextField in order to change the cursor's current position, the magnifying glass pops up as expected. But once I let go the tap, the keyboard closes immediately.

How do I solve this problem?

- (void)viewDidLoad
{
    ...
    TagDetailCell *cell1 = [[[NSBundle mainBundle] loadNibNamed:@"TagDetailCellView" owner:self options:nil] lastObject];
    TagDetailCell *cell2 = [[[NSBundle mainBundle] loadNibNamed:@"TagDetailCellView" owner:self options:nil] lastObject];
    TagDetailCell *cell3 = [[[NSBundle mainBundle] loadNibNamed:@"TagDetailCellView" owner:self options:nil] lastObject]; 
    self.cells=[NSArray arrayWithObjects:cell1, cell2, cell3, nil];
    ...
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[self.cells objectAtIndex:indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    switch (indexPath.row) {
        ...
        ((TagDetailCell*)cell).fieldLabel.text= ...;
        ((TagDetailCell*)cell).fieldValue.clearButtonMode=UITextFieldViewModeWhileEditing;
        ((TagDetailCell*)cell).fieldValue.text=...;
        [((TagDetailCell*)cell).fieldValue addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
        ...
    }
    return cell;

}

I didn't implement any delegates of UITextField. Should I?

Upvotes: 0

Views: 775

Answers (2)

Nimit Pattanasri
Nimit Pattanasri

Reputation: 1602

I was stupid. I subclass UITextField and override the following method in order to prevent user from copying/pasting texts:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender    
{    
    [UIMenuController sharedMenuController].menuVisible = NO;  //do not display the menu
    [self resignFirstResponder];                      //do not allow the user to selected anything
    return NO;
}

The problem of keyboard dismissing came from the call of [self resignFirstResponder]. Removing it, therefore, solves the issue.

Upvotes: 1

Gobot
Gobot

Reputation: 2480

Check out the UIViewController method disablesAutomaticKeyboardDismissal in the docs.
Quoted for Apple docs:

Override this method in a subclass to allow or disallow the dismissal of the current input view (usually the system keyboard) when changing from a control that wants the input view to one that does not. Under normal circumstances, when the user taps a control that requires an input view, the system automatically displays that view. Tapping in a control that does not want an input view subsequently causes the current input view to be dismissed but may not in all cases. You can override this method in those outstanding cases to allow the input view to be dismissed or use this method to prevent the view from being dismissed in other cases.

Upvotes: 0

Related Questions