我正在使用iOS中的应用程序向用户显示随机字符串,并要求用户通过键盘输入相同的字符串.我想要做的是比较用户输入的字符串,仅在他们按下"返回"键后.这可能吗?
以下是我的相关方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.tag == 1)
{
userString = @"";
userString = keypadText.text;
[keypadText resignFirstResponder];
//here 'rand' is the random string that was presented earlier to the user, and userString is the text that the user has entered.
if ([userString isEqualToString:rand]) {
//do something
}
else {
//do something else
}
[self.navigationController popViewControllerAnimated:YES];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,我只想在用户按下"返回"键后进行比较,直到那时,允许用户尽可能多地输入给定的文本,并进行必要的修正.
有人能告诉我怎么做吗?
只需使用textFieldDidFinishingEditing而不是shouldChangeCharactersinRange.
当然,也有
- (BOOL)textFieldShouldReturn:(UITextField *)textField
后者是更好的方式.
很简单.