Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Whis是在iPhone中添加活动指示器的正确方法_Iphone - Fatal编程技术网

Whis是在iPhone中添加活动指示器的正确方法

Whis是在iPhone中添加活动指示器的正确方法,iphone,Iphone,我需要显示活动指示器,但无法找到正确的方法,我尝试的是: 在ViewDidLoad中 [self-performSelectorOnMainThread:@selector(setupActivityIndicator),对象:nil waitUntilDone:NO] [self-userDataFromServer] [self-performSelectorOnMainThread:@selector(StopActivityIndicationInMainThread),对象:nil w

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

  • 在ViewDidLoad中
  • [self-performSelectorOnMainThread:@selector(setupActivityIndicator),对象:nil waitUntilDone:NO]

    [self-userDataFromServer]

    [self-performSelectorOnMainThread:@selector(StopActivityIndicationInMainThread),对象:nil waitUntilDone:NO]

    ----或-----

  • 在ViewDidLoad中
  • [自启动战术指示器]

    [self-userDataFromServer]

    [自停止活动指示器]


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

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

    我认为您可能需要在辅助线程上运行userDataFromServer。在viewDidLoad中尝试

    [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,您可以将活动指示器添加到
    viewdiload
    上的视图中,然后在执行
    [urlConnection start]
    时执行
    [activityIndicator startAnimating]
    。然后,当您到达
    -connectiondFinish
    时,您可以停止它。如果你明白的话


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

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

    - (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;
    }
    

    我正在发送:NSData*data=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];那是你的问题。。。不要发送同步请求-这会延迟iphone的反应。ie:它会暂时冻结UI-包括活动指示器。查找NSURLConnection的委托方法并使用
    NSURLConnection*conn=[NSURLConnection connectionWithRequest:urlRequest]
    有一个名为
    -connection:didReceiveData:
    的委托方法,您可以在其中
    [myData appendData:data]其中
    myData
    是一个全局
    NSMutableData
    变量:这样,它将在后台运行,不会中断你的应用程序的运行。我无法更改此类。请帮助我了解如何将活动指示器与SynchronousRequest一起使用。我试图使用NSAutoreleasePool,但它显示“自动释放,没有池在适当的位置-只是泄漏”。你不能这样做。。。问题是发送到屏幕的所有操作都是在主线程上完成的。如果您使用同步请求阻止该线程,那么活动指示器将不会显示/动画,直到您停止该请求-即:直到其完成。为什么你不能改变班级?它在网络层,如果我改变它,一切都会受到影响。我在我的项目的最后阶段,不希望在核心逻辑上有任何剧烈的变化。因为我已经实现了基于同步请求的所有业务功能。我不知道如果将其更改为asynchronous会发生什么:(在没有池的情况下自动释放-只是泄漏。它在日志中显示了这一点,我如何删除它,添加一个NSAutoreleasePool,如我上面所示,这将解决问题。将任何代码放在NSAutoreleasePool*pool…和[pool release]之间;