У меня есть UIDatePicker, отображаемый в popover. Когда я вернусь, в половине случаев значение не будет обновляться, то это будет каждый раз после этого. В другой половине он обновляется в первый раз и каждый раз после этого - как и ожидалось.UIDatePicker в popover случайным образом не обновляется в первый раз, но будет каждый раз после этого
Сценарий 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 и испытать ту же проблему.
Так что я добавить '[собственного performSelector: @selector (setDuration :) withObject: Datepicker afterDelay: 0];' в 'setupDatePickerWithIndexPath' и' - (Недействительными) setDuration: (UIDatePicker *) Datepicker {datePicker.countDownDuration = 60; } '. Это сделало проблему еще хуже, потому что она всегда имеет два оборота для обновления. –