在我的iOS应用程序中,我有一个用户填写的表单。我使用UIKeyboard
上的返回键移动到屏幕上的下一个UITextField
。
所以我像这样实施了textFieldShouldReturn
:
-(BOOL)textFieldShouldReturn:(BBTextField *)textField
{
//The textFields are in a UITableView with custom cells///
NSIndexPath *indexPath = [self.tableView indexPathForCellContaininView:textField];
if (self.selectedSegmentIndex == SegmentedControlStep1){
if (indexPath.section == 0){
switch (indexPath.row){
case 0:
[self.textFieldne becomeFirstResponder];
break;
case 1:
[self.textFieldTwo becomeFirstResponder];
break;
}
}
if (indexPath.section == 1){
switch (indexPath.row){
case 0:
[self.textFieldThree becomeFirstResponder];
break;
case 1:
[self.textFieldFour becomeFirstResponder];
break;
case 2:
[self.textFieldFive becomeFirstResponder];
break;
}
}
if (indexPath.section == 2){
switch (indexPath.row){
case 0:
[self.textFieldSix becomeFirstResponder];
break;
case 1:
[self.textFieldSeven becomeFirstResponder];
break;
case 2:
[self nextButtonPressed:nil];
break;
}
}
}
if (self.selectedSegmentIndex == SegmentedControlStep2){
switch (indexPath.row){
case 0:
[self.textFieldNine becomeFirstResponder];
break;
}
}
return YES;
}
此代码有效。但是,当调用textFieldDidEndEditing
时,我会验证某些文本字段的输入,并在验证失败时在屏幕上显示UIAlert。
我这样处理:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
/* Cancel registration */
if (buttonIndex == 1){
[self dismissViewControllerAnimated:YES completion:nil];
}
/* Okay button */
if (buttonIndex == 0){
if (self.viewModel.telephoneNumberFailedValidation){
[self.textFieldOne becomeFirstResponder];
}else if (self.viewModel.mobileNumberFailedValidation){
[self.textFieldTwo becomeFirstResponder];
}
}
}
所以所有内容都可以正常运行到目前为止。一旦UIAlertView
被解雇,就会发生以下情况:
答案 0 :(得分:1)
由于我无法弄清楚为什么textFieldDidEndEditing
被调用两次,所以我决定查看可能更合适的其他UITextField delegate
方法并找到这个gem:textFieldShouldEndEditing
- 它会询问textField
是否结束编辑。看起来像我的验证逻辑的完美位置。如果验证失败并且焦点仍然在NO
,我只需返回UITextField
,因此不需要UIAlertView's
{{1}方法:delegate
- 少代码!
这clickedButtonAtIndex
似乎是解决我问题的正确方法。