English 中文(简体)
Whis是在iPhone中添加活动指示器的正确方式
原标题:Whis is right way to add activity indicator in iPhone
  • 时间:2010-10-25 08:04:21
  •  标签:
  • iphone

我需要显示活动指标,但无法找到正确的方法,我尝试的是:

  1. In ViewDidLoad

[self-performSelectOnMainThread:@selector(setupActivityIndicator)withObject:nil waitUntilDone:NO]

[self-userDataFromServer]

[self-performSelectionOnMainThread:@selector(stopActivityIndicatorInMainThread)withObject:nil waitUntilDone:NO]

----或者-----

  1. In ViewDidLoad

[self-startActivityIndicator]

[self-userDataFromServer]

[self-stopActivityIndicator]

两人都在以这种不正确的方式工作。我们如何在并行线程上使用activityindicator?

最佳回答

根据我的经验,从服务器下载数据的最佳实践是

- (void)viewDidLoad {
   [self startActivityIndicator];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:
                              [NSURL URLWithString:aRequest]] delegate:self];
    [conn release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Store your data
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"ERROR DOWNLOADING");

    // Here you can display an UIAlert message

    // Then stop your activity indicator
    [self stopActivityIndicator];

    // Release the connection now that it s finished
    connection = nil;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"FINISH DOWNLOAD");

    // Or just stop
    [self stopActivityIndicator];

    // Do something

    // Release the connection now that it s finished
    connection = nil;
}
问题回答

没有必要在viewDidLoad中调用performSelectorOnMainThread:,因为您很可能已经在主线程上了。UIKit是UIActivityIndicator的一部分,无论如何都需要从主线程调用它。

我认为您可能想要做的是在辅助线程上运行userDataFromServer。在视图中DidLoad尝试

[self performSelectorInBackground:@selector(userDataFromServer) withObject:nil];

然后,在您的userDataFromServer方法中,确保包含一个NSAutoreleasePool

-(void)userDataFromServer {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        //code to actually get the data
       [pool release];
}

活动指示器只是显示有活动。

例如,如果您正在使用NSURLConnection在单独的线程上运行NSURLRequest,您将在viewDidLoad上向视图中添加活动指示符,然后在执行[urlConnection start]时执行[activityIndicator startAnimationing]

我假设你的问题是,你正在做的事情是将iphone“挂”在userDataFromServer中,所以最好在另一个线程上执行该选择器。你能把那个方法贴出来吗?如果您使用的是NSURLRequest,那么如果您添加[NSURLConnectionWithRequest:myRequest]并查找NSURLConnection委托方法,那么这对您来说是异步的。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。