解雇UIPopoverController的最快方法?

时间:2012-09-12 16:57:56

标签: ios ipad uipopovercontroller

我发布了一个来自UIPopoverController的通知回到我的主视图,然后立即调用dismissPopoverAnimated,然后继续做一些相当繁重的工作加载Web视图。所有这些代码都有效;问题是在一些较旧的ipads上,直到完成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;
    }
}

1 个答案:

答案 0 :(得分: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];

        });

    });
}