当有人在字段中输入文本时,我会调用一个IBAction。然后我验证输入。如果我确定有错误,我会显示一条消息,并希望用户再次进入同一个字段。而不是让他们选择文本字段来使键盘运行(这工作正常)我想让键盘显示。
我正在做[SymbolEntered becomeFirstResponder];
作为我IBAction中的最后一个声明,但键盘仍然消失了。我把它放在错误的地方吗?任何帮助,将不胜感激。感谢。
- (IBAction)textFieldDoneEditing:(id)sender {
DebugMsg.text = nil;
DebugMsg2.text = nil;
DebugMsg3.text = nil;
NSLog (@"done editing");
NSLog (@"%@", SymbolEntered.text);
if ([SymbolEntered.text isEqualToString:nil])
{
Result.textColor = [UIColor redColor];
Result.text = @"You must enter a symbol!";
[SymbolEntered becomeFirstResponder];
}
else
{
if ([SymbolEntered.text isEqualToString:
[NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]])
{
correct_count++;
Result.textColor = [UIColor greenColor];
Result.text = @"Correct!";
Score.hidden = FALSE;
Score.text = [NSString stringWithFormat:@"Score: %d out of %d - %d Percent", correct_count, el_count+1,
(correct_count*100)/(el_count+1)];
GetNextElementButton.hidden = FALSE;
SymbolEntered.enabled = FALSE;
el_count++;
attempts = max_tries + 1;
}
else
{
Score.hidden = TRUE;
Result.textColor = [UIColor redColor];
if (attempts < max_tries)
{
if (attempts+1 == max_tries)
{
Result.text = @"Sorry, one more try -";
}
else
{
Result.text = @"Sorry, try again - ";
}
GetNextElementButton.hidden = TRUE;
attempts++;
}
else
{
Result.text = [[NSString alloc] initWithFormat: @"Sorry. The correct answer is %@",
[NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]];
Score.hidden = FALSE;
Score.text = [NSString stringWithFormat:@"Score: %d out of %d - %d Percent", correct_count, el_count+1, (correct_count*100)/(el_count+1)];
GetNextElementButton.hidden = FALSE;
SymbolEntered.enabled = FALSE;
el_count++;
}
}
}
[SymbolEntered becomeFirstResponder];
NSLog (@"end of textfieldoneediting");
}
答案 0 :(得分:2)
您可以在UITextField textFieldShouldEndEditing:委托方法中进行验证。如果您从该回调中返回NO,则文本字段将保持第一响应者并且键盘不会消失。 (当然,你必须让你的控制器对象成为文本字段的委托。)
答案 1 :(得分:0)
稍后再尝试调用[textField becomeFirstResponder]?还要确保UITextField指针不是nil或其他东西。如需更多帮助,请展示一些代码,很难说出问题出在何处。
答案 2 :(得分:0)
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
尝试此操作,它将重新签名键盘。