代码没有被调用
Code is not getting called
在 shouldChnageTextInRange
的方法中,我有一段代码验证 textField
的货币输入。然后我尝试从 textField
获取 NSLog
,但我从未得到结果。
这是我从 this answer:
获得的编辑代码
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.myTextField)
{
static NSString *numbers = @"0123456789";
static NSString *numbersPeriod = @"01234567890.";
static NSString *numbersComma = @"0123456789,";
if (range.length > 0 && [string length] == 0) {
// enable delete
return YES;
}
NSString *symbol = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
if (range.location == 0 && [string isEqualToString:symbol]) {
// decimalseparator should not be first
return NO;
}
NSCharacterSet *characterSet;
NSRange separatorRange = [textField.text rangeOfString:symbol];
if (separatorRange.location == NSNotFound) {
if ([symbol isEqualToString:@"."]) {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersPeriod] invertedSet];
}
else {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersComma] invertedSet];
}
}
else {
// allow 2 characters after the decimal separator
if (range.location > (separatorRange.location + 2)) {
return NO;
}
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbers] invertedSet];
}
return ([[string stringByTrimmingCharactersInSet:characterSet] length] > 0);
}
NSLog(@"%@", textField.text);
}
最后一个 NSLog
永远不会被调用。我该怎么做才能解决这个问题? (我将 delegate
设置为 self。)
你应该把代码放在return之前。
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"%@", textField.text); //before return
...
}
once方法return它不执行下一行代码。在你的代码中一些小错误更正代码 NSLog(@"%@", textField.text);应该在代码 return ([[string stringByTrimmingCharactersInSet:characterSet] length] > 0);
之前
在 shouldChnageTextInRange
的方法中,我有一段代码验证 textField
的货币输入。然后我尝试从 textField
获取 NSLog
,但我从未得到结果。
这是我从 this answer:
获得的编辑代码- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.myTextField)
{
static NSString *numbers = @"0123456789";
static NSString *numbersPeriod = @"01234567890.";
static NSString *numbersComma = @"0123456789,";
if (range.length > 0 && [string length] == 0) {
// enable delete
return YES;
}
NSString *symbol = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
if (range.location == 0 && [string isEqualToString:symbol]) {
// decimalseparator should not be first
return NO;
}
NSCharacterSet *characterSet;
NSRange separatorRange = [textField.text rangeOfString:symbol];
if (separatorRange.location == NSNotFound) {
if ([symbol isEqualToString:@"."]) {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersPeriod] invertedSet];
}
else {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersComma] invertedSet];
}
}
else {
// allow 2 characters after the decimal separator
if (range.location > (separatorRange.location + 2)) {
return NO;
}
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbers] invertedSet];
}
return ([[string stringByTrimmingCharactersInSet:characterSet] length] > 0);
}
NSLog(@"%@", textField.text);
}
最后一个 NSLog
永远不会被调用。我该怎么做才能解决这个问题? (我将 delegate
设置为 self。)
你应该把代码放在return之前。
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"%@", textField.text); //before return
...
}
once方法return它不执行下一行代码。在你的代码中一些小错误更正代码 NSLog(@"%@", textField.text);应该在代码 return ([[string stringByTrimmingCharactersInSet:characterSet] length] > 0);
之前