【发布时间】:2014-06-30 02:59:30
【问题描述】:
昨天我在我的 iOS 应用程序中添加了一个UIActivityIndicatorView,一切都很好,现在我正在尝试运行同一个应用程序,但 UIActivityIndicatorView 不再显示,agenda 视图(调用 Web 服务) 需要很长时间(比昨天更长)才会出现,而且通常根本不会出现。请问我该如何解决这个问题?这是我的代码:
- (IBAction)agenda:(id)sender {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.hidesWhenStopped = YES;
[self.view addSubview:spinner];
[spinner startAnimating];
// how we stop refresh from freezing the main UI thread
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
// do our long running process here
// [NSThread sleepForTimeInterval:10];
AgendaViewController *agenda = [[ AgendaViewController alloc] initWithNibName:nil bundle:nil];
// do any UI stuff on the main UI thread
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[self.navigationController pushViewController:agenda animated:YES];
});
});
dispatch_release(downloadQueue);
}
【问题讨论】:
-
我觉得这段代码没问题。我建议注释掉
queue代码并检查微调器是否出现(不要stopAnimating)。想到的第一个罪魁祸首是您没有显示的“长时间运行的进程”代码。 -
当我注释
queue代码时,即使在队列代码未注释时使用[NSThread sleepForTimeInterval:10];,微调器也会显示,但当我添加此代码时它不会出现:AgendaViewController *agenda = [[ AgendaViewController alloc] initWithNibName:nil bundle:nil];似乎这行引起的问题是我要在“长时间运行的过程”中添加的代码有什么建议吗? -
尝试使用 dispatch_async(dispatch_get_main_queue() 块在主线程上添加微调器子视图。
-
好的,试着给你的
AgendaViewController一个标识符,然后像这样实例化你的对象。我假设您使用的是storyboards。这是example。 -
不,我使用的是 xib 文件
标签: ios objective-c uiactivityindicatorview