Do not pass cursor to next textfield without enter

2019-09-18 09:01发布

问题:

I have created textfields dynamically in UITableView I don't want to pass cursor without entering value in first textfield


[self.playerTable.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL*stop)
 {
     UITableViewCell *cell = obj;

     if([cell isKindOfClass:[UITableViewCell class]])
     {
         for(UITextField *textField in cell.contentView.subviews)
         {
             if([textField isKindOfClass:[UITextField class]])
             {
                 if ([textField isFirstResponder])
                 {
                     [textField resignFirstResponder];

                     isEditMode = NO;
                     if(!isEditMode && [playerstr length] > 0)
                     {

                         NSMutableArray *playerinfoArry = [dbWrapper getPlayerInfo];
                         for (Playerinfo *player in playerinfoArry)
                         {
                             if ([player.playername isEqualToString:playerstr])
                             {
                                 isPlayerExist = YES;
                                 isEditMode = !isEditMode;
                                 CustomAlert *alert = [[CustomAlert alloc] initWithTitle:@"" message:@"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:@""];
                                 [_nameField resignFirstResponder];
                                 [alert showInView:self.view];
                                 NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:selectedRow inSection:0];

                                 [_playerTable selectRowAtIndexPath:indexPath1 animated:YES  scrollPosition:UITableViewScrollPositionTop];
                                 return;
                            }
                        }
                    }
                }
            }
        }
    }
}];

回答1:

instead of looking for the first responder while trying to edit another UITextField, have you tried the other approach: not permitting the UITextField to resign as first responder. This could be something like:

 - (BOOL) textFieldShouldEndEditing:(UITextField *)textField{

        for(UITextField *otherTextField in self.view)
        {
            if ([otherTextField isKindOfClass:[UITextField class]] && [textField.text isEqualToString:otherTextField.text]){
                CustomAlert *alert = [[CustomAlert alloc] initWithTitle:@"" message:@"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:@""];
                [alert showInView:self.view];
                return NO;
            }
        }
    return YES;

}

I hope this helps.