项目中遇到了一个需求:
要求根据某一个UITextField.text
的值的改变随时的计算另一个UITextField.text
的值。
我们都知道监听UITextField
的值改变的几种方法:
1.通过UITextFieldDelegate
监听textField
的值的改变;
2.通过NSNotificationCenter
监听;
3.通过KVO
监听;
4.通过addarget:
直接监听;
5.通过三方库ReactiveCocoa
添加监听;
目前功能实现使用的监听方式是第四种直接监听,以下是代码实现:
#import "HHTableViewController.h"
@interface HHTableViewController ()
@property (weak, nonatomic) IBOutlet UILabel *totalLab;
@property (weak, nonatomic) IBOutlet UITextField *firstTF;
@property (weak, nonatomic) IBOutlet UITextField *secTF;
@property (weak, nonatomic) IBOutlet UILabel *msgLab;
@end
@implementation HHTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.firstTF addTarget:self action:@selector(textFieldWillChange:) forControlEvents:UIControlEventEditingChanged];
[self.secTF addTarget:self action:@selector(textFieldWillChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldWillChange:(UITextField *)textField {
if ([textField.text hasPrefix:@"."]) {
textField.text = [@"0" stringByAppendingString:textField.text];
}
if ([self validateMoney:textField.text]) {
if (textField == self.firstTF) {
if ([textField.text doubleValue] > [self.totalLab.text doubleValue]) {
self.msgLab.text = @"error,首付金额大于总价格!";
return;
}
self.secTF.text = [self decimalCalculateWithTotalValue:self.totalLab.text type:0 textValue:textField.text];
}
else {
if ([textField.text doubleValue] > 1.00) {
self.msgLab.text = @"首付所占利率比例最大为1";
return;
}
self.firstTF.text = [self decimalCalculateWithTotalValue:self.totalLab.text type:1 textValue:textField.text];
}
self.msgLab.text = @"成功!";
}
else {
if (textField == self.firstTF) {
self.secTF.text = @"";
self.msgLab.text = @"首付金额输入值有误!";
}
else {
self.firstTF.text = @"";
self.msgLab.text = @"换算比率输入值有误!";
}
}
}
-(BOOL)validateMoney:(NSString *)money
{
NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:money];
}
/**
* 根据总价计算首付及首付比率
* textValue: 目前处于编辑状态的textField的text值
* type: = 0 除
* = 1 乘
* totalValue: 总的价格
* return: 返回计算结果(String类型)
*/
- (NSString *)decimalCalculateWithTotalValue:(NSString *)totalValue type:(short)type textValue:(NSString *)textValue {
NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:textValue];
NSDecimalNumberHandler*roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:9 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
switch (type) {
case 0: // 除
{
double divideValue = [[decimal decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:totalValue] withBehavior:roundUp] doubleValue];
return [NSString stringWithFormat:@"%.2f", divideValue];
}
break;
case 1: // 乘
{
double multiplyValue = [[decimal decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:totalValue] withBehavior:roundUp] doubleValue];
return [NSString stringWithFormat:@"%.2f", multiplyValue];
}
break;
default:
break;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果示意图: