Uiactivityindicator没有显示在子视图中

时间:2013-04-07 12:01:52

标签: ios objective-c uiactivityindicatorview

我试图在主线程上的tableview控制器上的子视图中显示一个微调器。该视图仅在某些情况下显示,并且通常在我的后台线程完成后显示。这是我的代码:

[self.view addSubview:spinnerView];
        [self.view bringSubviewToFront:spinnerView];
        self.spinnerView.layer.cornerRadius = 8;
        self.spinnerView.center = CGPointMake(160, 150);

        [spinner startAnimating];

        dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
        dispatch_async(downloadQueue, ^ {
            dispatch_async(dispatch_get_main_queue(), ^{
                [NSThread sleepForTimeInterval:10];
                // Add recurring transactions if necessary
                if (tran.recur.id != 0) {
                    [Utilities addRecurringTransactionsUpToCurrentMonthByTransaction:tran];
                }

                [spinner stopAnimating];
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        });
        dispatch_release(downloadQueue);
    }
}

1 个答案:

答案 0 :(得分:0)

由于所有UI都在主线程上运行,因此必须调用performSelectorOnMainThread:withObject:waitUntilDone:如下所示:

    [self.view addSubview:spinnerView];
    [self.view bringSubviewToFront:spinnerView];
    self.spinnerView.layer.cornerRadius = 8;
    self.spinnerView.center = CGPointMake(160, 150);

    [spinner performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];


    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    dispatch_async(downloadQueue, ^ {
        dispatch_async(dispatch_get_main_queue(), ^{
            [NSThread sleepForTimeInterval:10];
            // Add recurring transactions if necessary
            if (tran.recur.id != 0) {
                [Utilities addRecurringTransactionsUpToCurrentMonthByTransaction:tran];
            }

            [spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];

            [self dismissViewControllerAnimated:YES completion:nil];
        });
    });
    dispatch_release(downloadQueue);