1

我将来自 UIPopoverController 的通知发布回我的主视图,然后立即调用dismissPopoverAnimated,然后开始做一些相当繁重的工作来加载 Web 视图。所有这些代码都有效;问题在于,在一些较旧的 ipad 上,直到 CPU 密集型工作完成(在调试器中验证)之后,popover 才会真正解除。这会导致弹出框在被解雇后似乎挂起一秒钟。如何确保立即关闭弹出窗口而不是先执行密集代码?

响应通知的方法如下:

- (void)changeDefaultView:(NSNotification *)note
{
    [self closePopover];

    int i;
    for(i = 0; i < [arrWebViewControllers count]; i++)
    {
        WebViewController *wvc = [arrWebViewControllers objectAtIndex:i];
        [[wvc webview] stopLoading];
        [[wvc webview] removeFromSuperview];
        [[wvc imageview] removeFromSuperview];
        wvc = nil;
    }

    [arrWebViewControllers removeAllObjects];
    [arrLinks removeAllObjects];
    [arrImageViews removeAllObjects];

    [self loadCategory:[note object]];

    [self addWidgetsToView];
}

而 closePopover 是:

- (void)closePopover
{
    if(popover != nil)
    {
        [popover dismissPopoverAnimated:YES];
        popover = nil;
    }
}
4

1 回答 1

0

消失的弹出框的动画发生在主运行循环上;如果您在主线程上执行繁重的 CPU 工作,那么循环将没有机会运行。所以你需要做的是在后台线程上执行你的工作。

目前首选的方法是使用 Grand Central Dispatch,如下所示:(我假设这loadCategory:是 CPU 密集型操作)

- (void)changeDefaultView:(NSNotification *)note
{
    [self closePopover];

    int i;
    for(i = 0; i < [arrWebViewControllers count]; i++)
    {
        WebViewController *wvc = [arrWebViewControllers objectAtIndex:i];
        [[wvc webview] stopLoading];
        [[wvc webview] removeFromSuperview];
        [[wvc imageview] removeFromSuperview];
        wvc = nil;
    }

    [arrWebViewControllers removeAllObjects];
    [arrLinks removeAllObjects];
    [arrImageViews removeAllObjects];

    // perform the expensive operation on a background thread
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        [self loadCategory:[note object]];

        // now get back onto the main thread to perform our UI update
        dispatch_async(dispatch_get_main_queue(), ^{

            [self addWidgetsToView];

        });

    });
}
于 2012-09-12T17:18:00.523 回答