我正在使用以下textfield delegate
验证用户条目。shouldChangeCharactersInRange行为异常
我们假设currentTotal
等于30.00
美元,并且每当用户输入two times
等于或大于currentTotal
时,我试图发出警报。
在我测试应用程序时,当用户输入63
美元时,没有警报发生,但只要用户输入630
美元,然后发出警报。
tip
和currentTotal
是double
。
我在做什么错,有什么建议?
- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([aTextField.text containsString:@"$"])
{
tip = [[aTextField.text stringByReplacingOccurrencesOfString:@"$" withString:@""] doubleValue];
}
else
{
tip = [aTextField.text doubleValue];
}
if(tip > currentTotal *2)
{
[self presentViewController:[AppConstant oneButtonDisplayAlert:@"Error" withMessage:@"Please enter valid tip"] animated:YES completion:nil];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.tipTF.text = @"$ ";
}
什么是您的currentTotal –
是30.00,双。 – hotspring
将double转换为integerValue并检查一次 –