-2
-(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 连接类,在我的视图控制器中,我像这样调用这个列表:“ 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& 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 " ..我该如何管理这个?有什么想法吗?在此先感谢 ..

4

1 回答 1

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];

}
于 2013-01-07T19:15:31.103 回答