How to programmatically add a simple default loading(progress) bar in iphone app

前端 未结 18 2578
你的背包
你的背包 2020-12-12 17:16

I am using http communication in My iPhone app. I want to show a progress bar while it is loading data from server. How can I do it programmatically?

I just want a d

18条回答
  •  醉酒成梦
    2020-12-12 17:49

    I'd recommend using NSURLConnection. The methods you would need are:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
      [self.resourceData setLength:0];
      self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
      [self.resourceData appendData:data];
      NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
      self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
      self.progressBar.hidden = YES;
    }
    

    And the header file:

    @property (nonatomic, retain) UIProgressView *progressBar;
    @property (nonatomic, retain) NSMutableData *resourceData;
    @property (nonatomic, retain) NSNumber *filesize;
    

    Hope it helps

提交回复
热议问题