带有UIActivityIndi​​catorView的活动指示器(微调器)

Cy.*_*Cy. 4 iphone cocoa-touch objective-c

我有一个tableView加载XML feed,如下所示:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([stories count] == 0) {
        NSString *path = @"http://myurl.com/file.xml";
        [self parseXMLFileAtURL:path];
    }
}
Run Code Online (Sandbox Code Playgroud)

我想让微调器显示在应用程序启动的顶部栏上,并在数据显示在我的tableView上后消失.

我认为将开头放在viewDidAppear和结束处,-(void)parserDidEndDocument:(NSXMLParser *)parser 但它没有用.

我很欣赏有关如何实施此解决方案的解决方案.

Dav*_*ong 5

这是问题所在:NSXMLParser是同步API.这意味着一旦你调用parse你的话NSXMLParser,那个线程将完全解析xml,这意味着没有UI更新.

以下是我通常如何解决这个问题:

- (void) startThingsUp {
  //put the spinner onto the screen
  //start the spinner animating

  NSString *path = @"http://myurl.com/file.xml";
  [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
}

- (void) parseXMLFileAtURL:(NSString *)path {
  //do stuff
  [xmlParser parse];
  [self performSelectorOnMainThread:@selector(doneParsing) withObject:nil waitUntilDone:NO];
}

- (void) doneParsing {
  //stop the spinner
  //remove it from the screen
}
Run Code Online (Sandbox Code Playgroud)

我多次使用过这种方法,效果很好.