Iphone I';I’’我想知道iOS键盘的时间
这就是时间安排的情况。我有一个UILabel,每次键盘更新UITextField时都要更新它。我有两个uitextfield,但只有一个是第一响应者,所以不要担心有两个uitextfield用于后端。问题在于UILabel更新和UITextField委托函数的计时Iphone I';I’’我想知道iOS键盘的时间,iphone,ios,objective-c,Iphone,Ios,Objective C,这就是时间安排的情况。我有一个UILabel,每次键盘更新UITextField时都要更新它。我有两个uitextfield,但只有一个是第一响应者,所以不要担心有两个uitextfield用于后端。问题在于UILabel更新和UITextField委托函数的计时 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *
- (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];
}
仔细考虑几分钟后,这可能是一个运行循环问题。在调用方法更新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...];
});
});
对。
textField:shouldChangeCharactersRange:replacementString:
中的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;
}
或者添加在编辑发生后调用的iAction:
- (void)viewDidLoad {
// your viewDidLoad implementation
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (IBAction)textFieldDidChange:(UITextField *)sender {
[self syncTextFieldsMinutesAndHours: sender.text];
}
“在上述函数返回YES之前,不会添加替换字符串。”您能否澄清一下?我看到,每当您从该方法返回NO时,您似乎没有操纵标签,因此我不确定这是您的问题陈述还是您的需求陈述。如果是后者,您能解释一下您的问题吗?为什么要通过
shouldChangeCharactersInRange
更新标签?为什么不在文本字段实际更改时更新标签?为什么不根据文本字段的全文更新标签呢?您也根本没有处理范围。如果用户选择并替换字符怎么办?您能解释一下此功能的用途吗?使用“隐藏文本字段”可能不是最好的方法。我正在尝试按照您的代码进行操作,如果文本字段中没有正确的字符串,那么您什么也不做。但是,如果文本字段中包含正确的字符串,则可以修改文本字段的内容。也许我们可以建议一个更好的方法。我喜欢这个答案。我认为我的不是你想要的,但是谢谢你的投票@MichaelChoi