加载进度条IOS

时间:2013-01-07 18:57:04

标签: ios progress-bar progress

-(void)startConnection
{ 
    NSString *urlString = GET_EVENTLIST_URL_STRING;

    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];
    [urlString UTF8String];


    if (!url)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@", GET_EVENTLIST_URL_STRING];
        [self.delegate didGetEventListInCorrect:reason];
        return;
    }

    theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];

    // Set the HTTP method of the request to POST
    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody:[urlString dataUsingEncoding:NSUTF16StringEncoding]];



    if (!theRequest)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@", GET_EVENTLIST_URL_STRING];
        [self.delegate didGetEventListInCorrect:reason];
        return;
    }
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection)
    {
        NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@", GET_EVENTLIST_URL_STRING];
        [self.delegate didGetEventListInCorrect:reason];
        return;
    }

    if (theConnection)
    {
        myData = [[NSMutableData alloc]init];

    }

}

#pragma mark - Methods connection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    // receivedData is an instance variable declared elsewhere.
    [myData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [myData appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // finished downloading the data, cleaning up
    [self.delegate didGetEventListCorrect:myData ];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.delegate didGetEventListInCorrect:@"Failed Connection"];
}

#pragma mark - Main method of LoginUrlConnection with NSURLRequest and NSURLConnection
+ (id) connectionGetEventList:(id<GetEventListURLConnectionDelegate>)aDelegate
{
    EventListURLConnection *getEventListUrlConnection = [[self alloc]init];

    getEventListUrlConnection.delegate = aDelegate;

    [getEventListUrlConnection startConnection];

    return getEventListUrlConnection;
}

大家好, 这是我的url连接类,在我的viewcontroller中我调用这个列表:“eventconnection = [EventListURLConnection connectionGetEventList:self]; “..我需要使用一个显示加载过程的进度条,如”http://www.google.com.tr/imgres?hl=en&sa=X&tbo=d&biw=1063&bih=502&tbm=isch&tbnid=3fZwJc8SC_xoPM:&imgrefurl=http://www.hongkiat.com/blog/most-wanted-freebies-web-designers/&docid=VLaTiYV3nRfeXM&imgurl=http://media02.hongkiat.com/freebies-for-web-designers-2011/progress-bar.jpg&w=580&h=250&ei=jxnrUMWpOKb24QTn54CQCg&zoom=1&iact=hc&vpx=698&vpy=185&dur=704&hovh=148&hovw=342&tx=162&ty=102&sig=114441653047551513554&page=2&tbnh=147&tbnw=319&start=16&ndsp=14&ved=1t:429,r:19,s:0,i:142“。我如何管理这个?有任何想法吗?提前感谢..

1 个答案:

答案 0 :(得分:1)

您可以使用UISlider跟踪负载的进度。因此,假设您使用myData来保存内容,FILE_SIZE是要下载的内容的字节大小,progressView是UISlider(实际上它也可以是包含UISlider子视图的自定义视图和指定下载%的UILabel子视图),然后下面的代码片段应该这样做......

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.myData appendData:data];
    float percentDownloaded = (float)[self.myData length]/FILE_SIZE;
    NSLog (@"PERCENT DOWNLOADED IS %d %f : %f",[self.myData length], percentDownloaded,percentDownloaded*100);
    [self.progressView setProgress:percentDownloaded];

}
相关问题