0

我创建了一个只有一页的应用程序。在该页面中有一个按钮、标签和进度视图。我想要的是在单击按钮并显示进度和标签状态下载时开始下载。

我可以制作进度视图,但无法制作显示下载状态的标签。

例如,我希望我的标签显示"12 MB downloaded of 100 MB"

这是我的代码:

查看控制器.h

@interface ViewController : UIViewController
{
    float expectedBytes;
}
@property (weak,nonatomic) IBOutlet UIProgressView *progressView;
@property (strong,nonatomic) IBOutlet UIButton *download;
- (IBAction)download:(id)sender;

@end

视图控制器.m

@implementation ViewController
{
    NSMutableData *receivedData;
    NSString *currentURL;
    NSString *name;
}
- (void)viewDidLoad
{
    currentURL = @"http://192.168.1.100/mamal/Adele%20-%20Someone%20Like%20You%20(MTV%20Video%20Music%20Awards%202011)%20HD%20Live.mkv";
    name = [currentURL lastPathComponent];
    [super viewDidLoad];
}
@synthesize progressView,download;
-(IBAction)download:(id)sender
{
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:name];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    if (!fileExists)
    {
        [self downloadWithNsurlconnection];
    }
    else
    {
        NSLog(@"FILE EXIST");
    }
}
-(void)downloadWithNsurlconnection
{
    NSURL *url = [NSURL URLWithString:currentURL];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:600];
    receivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    [connection start];

}


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    progressView.hidden = NO;
    [receivedData setLength:0];
    expectedBytes = [response expectedContentLength];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [progressView setProgress:progressive];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",paths);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:name];
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [receivedData writeToFile:pdfPath atomically:YES];
}

请指导我如何UILabel使用NSURLConnection.

4

2 回答 2

0

将此语法放在 .h 文件中

@property (weak,nonatomic) IBOutlet UILabel *label;

然后在 .m 文件中

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [progressView setProgress:progressive];
    [lable setText:[NSString stringWithFormat:@"%u MB of %f MB",[receivedData length],expectedBytes]];

}
于 2013-05-09T09:36:22.223 回答
0

为标签创建一个出口

@property (weak,nonatomic) IBOutlet UILabel *label;

然后在

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [progressView setProgress:progressive];
    [lable setText:[NSString stringWithFormat:@"%u MB of %f MB",[receivedData length],expectedBytes]];
}

您可以将字节更改为 MB

于 2013-05-09T08:32:53.547 回答