所以这里是时间的情况。我有一个 UILabel,每次键盘更新 UITextField 时我都想更新它。我有两个 UITextField,但只有一个是第一响应者,所以不要担心有两个我有它们用于后端目的。问题是来自 UILabel 更新和 UITextField 委托函数的时间
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;˚
在上述函数返回 YES 之前,不会添加替换字符串。我需要在调用此函数之后或在此函数期间更新我的标签。我似乎无法弄清楚它将如何工作。UILabel 总是落后一个字符。以下是本节的一般代码。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([self.hiddenTextFieldForTimeMinutes.text length] == 2 && [self.hiddenTextFieldForTime.text length] == 2 && ![string isEqualToString:@""])
{
return NO;
}
[self syncTextFieldsMinutesAndHours: string];
// This returns the default Yes;
return YES;
}
- (void) setAccessoryLabel: (NSString *) hourString minutesString: (NSString *) minuteString
{
timeAccessoryLabel.text = [NSString stringWithFormat:@"%@:%@", hourString, minuteString];
}
- (void) syncTextFieldsMinutesAndHours: (NSString *) string
{
// These are the textFields
NSMutableString *hoursString = [NSMutableString stringWithFormat: @"%@", self.hiddenTextFieldForTime.text];
NSMutableString *minutesString = [NSMutableString stringWithFormat: @"%@", self.hiddenTextFieldForTimeMinutes.text];
if([self.hiddenTextFieldForTimeMinutes.text length] == 2 && ![string isEqualToString: @""])
{
[hoursString appendString: [NSString stringWithFormat:@"%c", [minutesString characterAtIndex:0]]];
[self.hiddenTextFieldForTime setText:[NSString stringWithFormat:@"%@", hoursString]];
[self.hiddenTextFieldForTimeMinutes setText: [self.hiddenTextFieldForTimeMinutes.text substringFromIndex:1]];
} else if([self.hiddenTextFieldForTimeMinutes.text length] == 2 && [string isEqualToString: @""])
{
// Hours has nothing in it
if([hoursString length] == 0)
{
return;
} else if([hoursString length] == 1)
{
// Since the timing of the add and remove of the string is done by return of the delegate we append the string to the beginning first then return.
[self.hiddenTextFieldForTimeMinutes setText: [NSString stringWithFormat:@"%c%@", [self.hiddenTextFieldForTime.text characterAtIndex:0], self.hiddenTextFieldForTimeMinutes.text]];
[self.hiddenTextFieldForTime setText:@""];
} else if ([hoursString length] == 2)
{
// Since the timing of the add and remove of the string is done by return of the delegate we append the string to the beginning first then return.
[self.hiddenTextFieldForTimeMinutes setText: [NSString stringWithFormat:@"%c%@", [self.hiddenTextFieldForTime.text characterAtIndex:1], self.hiddenTextFieldForTimeMinutes.text]];
[self.hiddenTextFieldForTime setText: [NSString stringWithFormat:@"%c", [self.hiddenTextFieldForTime.text characterAtIndex:0]]];
}
}
[self setAccessoryLabel: self.hiddenTextFieldForTime.text minutesString:self.hiddenTextFieldForTimeMinutes.text];
}