我正在试图弄清楚iOS键盘的时机

时间:2013-08-28 18:23:39

标签: iphone ios objective-c

所以这是时间的情况。我有一个UILabel,我想在每次键盘更新UITextField时更新。我有两个UITextFields,但只有一个是第一个响应者,所以不要担心有两个我有它们用于后端目的。问题是来自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];
}

2 个答案:

答案 0 :(得分: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...];
    });
});

答案 1 :(得分:1)

是肯定的。 textField:shouldChangeCharactersInRange:replacementString:中textField的文本仍然具有旧值,因为只有在文本更改时才对问题回答“是”后才会更改。

您有两种选择。

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

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