1

设想:

UIScrollView 有一个 UIButton。点击 UIButton 会导致 UIPopoverController 从指定的矩形呈现自己。UIPopoverController 的 contentViewController 是一个带有 UIDatePicker 的 UIViewController。

点击按钮时触发的代码(按钮是自定义类型,在点击内注册了修饰):

- (void)showCalendar:(id)sender {
    dateViewController = [[UIViewController alloc] init];

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 240, 200)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];

    [datePicker addTarget:self
                   action:@selector(DateChanged:)
         forControlEvents:UIControlEventValueChanged];

    NSLocale *US = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setLocale:US];
    [datePicker setCalendar:cal];
    [datePicker setMaximumDate:[NSDate date]];


    dateViewController.view = datePicker;

    dateViewController.contentSizeForViewInPopover = CGSizeMake(240, 200);
    self.popoverController = [[UIPopoverController alloc] 
                               initWithContentViewController:dateViewController];
    self.popoverController.delegate = self;

    NSLog(@"YESYESYES");

        [self.popoverController presentPopoverFromRect:CGRectMake(753, 513, 1, 1) inView:parentView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

    NSLog(@"NONONO");

}

当我将设备连接到 Xcode 并运行应用程序时,两个 NSLog 语句之间存在明显的 1 秒或更长时间的延迟。这种滞后的原因可能是什么?

4

2 回答 2

2

通过更换解决了问题

dateViewController.view = datePicker;

[dateViewController.view addSubview:datePicker];
于 2012-09-04T11:35:47.107 回答
1

您在哪个队列中运行此方法?

要在主队列中运行它,试试这个。如果您没有使用 Interface Builder,请将 IB 代码替换为适当的代码。

- (void)showCalendar:(id)sender {

    // your setup code here... then:

    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"YESYESYES");
        [self.popoverController presentPopoverFromRect:CGRectMake(753, 513, 1, 1) inView:parentView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
        NSLog(@"NONONO");
    });
}
于 2012-08-02T06:59:52.283 回答