1

所以这里是时间的情况。我有一个 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];
}
4

2 回答 2

1

考虑几分钟后,这可能是一个运行循环问题。在调用更新 UILabel 的方法之前尝试添加:

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
//update label
[self updateLabelWithText:foo andText:bar];

或者尝试使用 GCD:

dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
    dispatch_async(main_queue, ^{
    // UI Updates here
    [self updateLabel...];
    });
});
于 2013-08-28T19:02:01.563 回答
1

是的。文本字段中的文本textField:shouldChangeCharactersInRange:replacementString: 仍将具有旧值,因为它只有在您对问题回答“是”后才会更改文本是否应该更改。

你有两个选择。

创建NSString您自己返回 YES 后您的 textField 将拥有的:

- (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;
    }

    NSString *realString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self syncTextFieldsMinutesAndHours: realString];

    // This returns the default Yes;
    return YES;
}

或添加在编辑发生后调用的 IBAction:

- (void)viewDidLoad {
    // your viewDidLoad implementation
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (IBAction)textFieldDidChange:(UITextField *)sender {
    [self syncTextFieldsMinutesAndHours: sender.text];
}
于 2013-08-28T19:13:52.957 回答