【发布时间】:2010-03-24 13:39:02
【问题描述】:
我正在开发一个 iPhone 应用程序,该应用程序具有基于标签栏的导航,带有五个标签。每个选项卡都包含一个 UITableView,其数据是远程检索的。理想情况下,我想使用在此远程检索期间启动/停止的单个 UIActivityIndicatorView(窗口的子视图) - 每个选项卡一次。
这是我在 AppDelegate 中设置微调器的方法:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:rootController.view];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setCenter:CGPointMake(160, 200)];
[window addSubview:activityIndicator];
[window makeKeyAndVisible];
}
由于我的选项卡都执行类似的功能,我创建了一个基类,我的所有选项卡的 ViewController 都继承自该基类。这是我用来进行远程检索的方法:
- (void)parseXMLFileAtURL:(NSString *)URL {
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"parseXMLFileAtURL started.");
[appDelegate.activityIndicator startAnimating];
NSLog(@"appDelegate.activityIndicator: %@", appDelegate.activityIndicator);
articles = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
NSLog(@"parseXMLFileAtURL finished.");
[appDelegate.activityIndicator stopAnimating];
[apool release];
}
这个方法被每个视图控制器调用如下:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([articles count] == 0) {
NSString *path = @"http://www.myproject.com/rss1.xml";
[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
}
}
这在应用程序加载第一个选项卡的内容时非常有用。我看到的是空桌子和旋转器。内容一加载,微调器就会消失。
奇怪的是,当我单击第二个选项卡时,来自 -parseXMLFileAtURL: 方法的 NSLog 消息显示在日志中,但屏幕挂在第一个选项卡的视图上,我看不到微调器。内容下载完成后,就会出现第二个选项卡的视图。
我怀疑这与线程有关,我仍然很熟悉。我在这里做错了什么吗?
【问题讨论】:
标签: iphone iphone-sdk-3.0