代码之家  ›  专栏  ›  技术社区  ›  N Fard

活动指示器从加载结束时开始

  •  0
  • N Fard  · 技术社区  · 15 年前

    我的代码怎么了?

    我希望当view1.nib开始加载时自旋体指示器开始。 所以我把[spinner startanimating];放在-(void)viewdidload中。 但它会得到那个URL,然后启动spinner指示器…

    - (void)viewDidLoad {
    
    
    
    
    
    
    
    
    [spinner startAnimating];
    
    NSURL *originalUrl=[NSURL URLWithString:@"http://example.com/"];
    NSData *data=nil;  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    NSURLResponse *response;
    NSError *error;
    data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    
    NSURL *LastURL=[response URL];
    
    
    NSLog(@"%@",LastURL);
    
    
    
    [backr,backrm,downloadr  setEnabled:FALSE];
    
    backr.hidden=YES;
    backrm.hidden=YES;
    downloadr.hidden=YES;
    [self nextr:0];
    [super viewDidLoad];
    

    }

    2 回复  |  直到 15 年前
        1
  •  1
  •   Yannick Loriot    15 年前

    您的问题是使用sendSynchronousRequest阻塞了主线程。 当数据下载的时候,你的线程被阻塞了,你的动画也被阻塞了,当请求完成后,动画继续。

    我应该使用connectionWithRequest:delegate:或initWithRequest:delegate:方法并将委托设置为self。 您可以在此处找到更多信息: Using NSURLConnection


    编辑:

    例子:

    在界面中定义它:

    @interface YourInterface {
    @private
       NSMutableData *receivedData;
    }
    

    然后在控制器中加载到视图中:

    // your previous definition of your NSMutableRequest
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (theConnection) {
        // Create the NSMutableData that will hold
        // the received data
        // receivedData is declared as a method instance elsewhere
        receivedData=[[NSMutableData data] retain];
    } else {
        // inform the user that the download could not be made
    }
    

    要完成,再次在控制器中实现以下方法:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        // append the new data to the receivedData
        // receivedData is declared as a method instance elsewhere
        [receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        // release the connection, and the data object
        [connection release];
    
        // receivedData is declared as a method instance elsewhere
        [receivedData release];
    
        // inform the user
        NSLog(@"Connection failed! Error - %@ %@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // do something with the data
        // receivedData is declared as a method instance elsewhere
        NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    
        // release the connection, and the data object
        [connection release];
        [receivedData release];
    }
    
        2
  •  1
  •   Tom Irving    15 年前

    您正在主线程上同步下载数据,因此UI将挂起,直到操作完成。这就是为什么你的旋转只能在完成时显示。

    要么在后台线程中运行下载,要么使用nsurlconnection的异步方法。