我在弹出窗口中显示了一个UIDatePicker。当我旋转时,值的一半时间不会更新,那么每次都会更新。另一半,它在第一次和每次之后更新 - 正如预期的那样。
场景1:点击,选择时间,更新,更新值,再次更新,再次更新值...重复。
场景2:点击,选择时间,更新,值不更新,再次更新,值更新,再次更新,值更新...重复。
起初,它每次都是第一次跳过更新,然后实施this answer's fix似乎有所帮助,直到我更多地使用它看到它现在只有一半时间起作用。
@property (nonatomic, strong) UIViewController *datePickerController;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == kAddEditProductsTargetBatchTime) {
[self setupDatePickerWithIndexPath:indexPath];
}
}
- (void)setupDatePickerWithIndexPath:(NSIndexPath *)indexPath {
CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath];
CGRect popoverFrame = CGRectMake(cellFrame.size.width - 100.0f, cellFrame.origin.y, 50.0f, cellFrame.size.height);
CGSize dpSize = CGSizeMake(200, 180);
_datePickerController = [[UIViewController alloc] init];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
datePicker.frame = CGRectMake(0,0,dpSize.width, dpSize.height);
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.countDownDuration = 0;
batchTarget *defaultCycleTarget = [cycleTargetRepository defaultCycleTarget:_product.cycleTargets]; //NSTimeInterval
if (indexPath.row == kAddEditProductsTargetBatchTime) {
NSDateComponents *dateComp = [NSDateComponents new];
dateComp.hour = [NSNumberFormatter hoursComponent:defaultCycleTarget.targetBatchTime]; //NSTimeInterval
dateComp.minute = [NSNumberFormatter minutesComponent:defaultCycleTarget.targetBatchTime]; //NSTimeInterval
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *pickerDate = [calendar dateFromComponents:dateComp];
[datePicker setDate:pickerDate animated: YES];
[datePicker addTarget:self action:@selector(batchTimeChanged:) forControlEvents:UIControlEventValueChanged];
}
_datePickerController.view = datePicker;
_datePickerController.modalPresentationStyle = UIModalPresentationPopover;
_datePickerController.preferredContentSize = dpSize;
[self presentViewController:_datePickerController animated:YES completion:nil];
_datePickerController.popoverPresentationController.sourceRect = popoverFrame;
_datePickerController.popoverPresentationController.sourceView = self.view;
}
//timeChanged doesn't even get fired off.. randomly && only on the first try
- (void)timeChanged:(UIDatePicker *)sender {
batchTarget *defaultCycleTarget = [cycleTargetRepository defaultCycleTarget:_product.cycleTargets];
if (defaultCycleTarget) {
defaultCycleTarget.targetBatchTime = sender.countDownDuration;
} else {
// If no defaultCycleTarget we need to initialize the data
batchTarget *newCycleTarget = [batchTarget new];
newCycleTarget.targetBatchTime = sender.countDownDuration;
_product.cycleTargets = [NSArray arrayWithObject:newCycleTarget];
}
[self.tableView reloadData];
}
我基本上有same exact code in a different view using Swift并遇到同样的问题。
答案 0 :(得分:0)
似乎是iOS缓冲区问题。
尝试更改runloop并使其达到60秒的倍数。它对我有用。
- (void) someMethodName
{
[self performSelector:@selector(sel:) withObject:datePicker afterDelay:0];
}
- (void) sel:(UIDatePicker *)datePicker
{
datePicker.countDownDuration = 60;
}