2010-11-23 15 views
5

पर NSURLConnection लोडिंग प्रक्रिया जोड़ें मैंने UIProgressView बनाया है। लेकिन मैंने NSTimerUIProgressView's प्रक्रिया का उपयोग किया। यूआरएल लोड होने पर अब मुझे UIProgressView प्रक्रिया को एकीकृत करने की आवश्यकता है। UIProgressView's आकार NSURLConnection's डेटा पर निर्भर करेगा।UIProgressView

मैंने निम्नलिखित कोड NSURLConnection पर उपयोग किया। _totalFileSize = response.expectedContentLength;:

-(void)load { 
    NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 

    UIAlertView *alert = [[UIAlertView alloc] init]; 
    [alert setTitle:@"Warning"]; 
    [alert setMessage:@"Network Connection Failed?"]; 
    [alert setDelegate:self]; 
    [alert addButtonWithTitle:@"Yes"]; 

    [alert show]; 
    [alert release]; 

    NSLog(@"Error"); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    responsetext = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 
} 

उत्तर

15

अपने didReceiveResponse समारोह में आप इतने तरह कुल फ़ाइल आकार मिल सकता है। या तो MyProgressBar.progress = _receivedDataBytes/(float)_totalFileSize

(: _receivedDataBytes += [data length];

अब सही आकार आप बस कर सकते हैं करने के लिए प्रगति बार सेट करने के लिए

:

अपने didReceiveData में आप तो कुल बाइट्स प्राप्त काउंटर को जोड़ सकते हैं में कार्य didReceiveData फ़ंक्शन या आपके कोड में कहीं और)

अपनी कक्षा में बाइट्स की संख्या रखने वाले चर जोड़ने के लिए मत भूलना!

मुझे आशा है कि इस मदद करता है ..

संपादित करें: यहाँ कैसे आप क्रम में अद्यतन करने के लिए progressview

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    _totalFileSize = response.expectedContentLength; 
    responseData = [[NSMutableData alloc] init]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    _receivedDataBytes += [data length]; 
    MyProgressBar.progress = _receivedDataBytes/(float)_totalFileSize; 
    [responseData appendData:data]; 
} 
+0

कैसे didReceiveResponse में प्रतिक्रिया घोषित करने के लिए, कृपया निम्न कोड को संपादित प्रतिनिधियों को लागू कर सकता है। - (शून्य) कनेक्शन: (NSURLConnection *) कनेक्शन didReceiveData: (NSData *) डेटा { \t कुल FileSize = response.expectedContentLength; \t प्राप्तडेटाइट्स + = [डेटा लंबाई]; \t एनएसएलओजी (@ "कुलफाइल आकार/कुलफाइल आकार =% एफ", प्राप्तडेटाइट्स/कुलफाइल आकार); \t [प्रतिक्रिया डेटा संलग्न करें डेटा: डेटा]; } – Velmurugan

+0

didReceiveResponse और didReceiveData 2 अलग-अलग प्रतिनिधि हैं। यह होना चाहिए: - (शून्य) कनेक्शन: (NSURLConnection *) कनेक्शन didReceiveResponse: (NSURLResponse *) प्रतिक्रिया { कुल FileSize = response.expectedContentLength; } और उस रेखा को हटा दें ReeceiveData – Hless

+0

मेरा उत्तर संपादित करें। मैं यह उल्लेख करना भूल गया था कि आपके द्वारा विभाजित मूल्यों में से एक को एक फ्लोट पर डाला जाना चाहिए। चूंकि आप फ्लोट परिशुद्धता के बिना अन्यथा पूर्णांक के साथ समाप्त हो सकते हैं। – Hless