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?
I was stupid. I subclass UITextField and override the following method in order to prevent user from copying/pasting texts:
The problem of keyboard dismissing came from the call of
[self resignFirstResponder]. Removing it, therefore, solves the issue.