0

我正在编写一个 IOS 文件上传应用程序,它NSUrlConnection一次打开多个(每个文件上传一个文件),并希望为每个文件实现相应的进度条。

NSUrlConnection文件上传片段:

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                  delegate:self

[urlConnection start];

更新进度条的委托方法:

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
self.progressBar.progress += (float)bytesWritten/(float)totalBytesExpectedToWrite;
NSLog(@"it's working: %lf",self.progressBar.progress);
}

现在在这种情况下,如果我为每个文件上传都有一个单独的进度条,有没有办法知道哪个文件上传,即NSURLConnection这个委托对应于哪个,以便我可以相应地更新正确的进度条。是否有任何我可以设置的属性,我可以NSURLConnection在委托方法中访问连接变量?

谢谢。

4

1 回答 1

0

我所做的是我构建了自己的类(例如 MyUploader),并在其中添加了 NSURLConnection 对象,同时也是对进度条控件的引用,该控件将由该 NSURLConnection 对象更新。

@interface MyUploader

@property (strong, nonatomic) NSURLConnection* connection;
@Property (weak, nonatomic) UIProgressBar bar;

- (void)startUploadingFile:(NSString*)filePath withBarToUpdate:(UIProgressBar*)progressBar;

@end 

在您的视图控制器中,您可以在 startUploadingFile:withBarToUpdate: 中传递 bar 对象和文件路径,它可以存储 bar 对象并启动连接。

此外,此类应实现 NSURLConnectionDelegate 以更新进度条,就像您在代码中所做的那样

于 2015-02-26T12:28:16.823 回答