【问题标题】:Loading Progress Bar IOS加载进度条IOS
【发布时间】:2013-01-07 18:57:03
【问题描述】:
-(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&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 ” .. 我该如何管理?任何想法?提前谢谢..

【问题讨论】:

  • 您可以使用第三方网络库,例如 AFNetworking,它支持进度条等等。

标签: ios progress-bar progress


【解决方案1】:

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

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

}

【讨论】:

  • 好的,但是,你写的这段代码是为 nsobject 类写的。那么我如何在 viewcontroller 中管理呢?
  • 您可以使用 iOS 中可用的任何通知机制来通知管理百分比下载进度视图的 ViewController - 例如使用 KVO 在对象上设置 percentDownloaded 属性。 viewController 观察这个属性并相应地更新 UI。这与基于模型更改更新 UI 的典型 MVC 模型没有什么不同。
  • 我无法获得:S 你能解释一下吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2020-05-13
  • 1970-01-01
  • 2018-04-13
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多