我有一个DownloadSessionDelegate类来处理大文件的下载过程。 我想在进度视图中展示进展。有关下载状态的信息位于我的DownloadSessionDelegate类中。现在我不知道如何在课程之外更新我的进度视图。
如何做到这一点?
class DownloadSessionDelegate : NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var handlerQueue: [String : CompleteHandlerBlock]!
...
...
...
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")
progressView.setProgress(0.5, animated: true); // <<<Howto reference the progressView
}
}
从我的ViewController.swift触发下载:
func download_zip(sURL: String, sToLocation: String) {
let progressView = UIProgressView(progressViewStyle: .Bar);
progressView.center = view.center;
progressView.progress = 1/2;
progressView.trackTintColor = UIColor.lightGrayColor();
progressView.tintColor=UIColor.blueColor();
view.addSubview(progressView);
var delegate = DownloadSessionDelegate.sharedInstance;
delegate.storePath=sToLocation;
struct SessionProperties {
static let identifier : String! = "url_session_background_download"
}
var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
var url = NSURLRequest(URL: NSURL(string: sURL)!)
var downloadTask = backgroundSession.downloadTaskWithRequest(url)
downloadTask.resume()
}
答案 0 :(得分:0)
要从另一个类引用进度视图,您需要将进度视图的实例传递给需要其引用的类,在您的情况下:
class DownloadSessionDelegate : NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var handlerQueue: [String : CompleteHandlerBlock]!
var progressView: UIProgressView!
...
...
...
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")
progressView.setProgress(0.5, animated: true); // <<<Howto reference the progressView
}
}
ViewContoller
func download_zip(sURL: String, sToLocation: String) {
let progressView = UIProgressView(progressViewStyle: .Bar);
progressView.center = view.center;
progressView.progress = 1/2;
progressView.trackTintColor = UIColor.lightGrayColor();
progressView.tintColor=UIColor.blueColor();
view.addSubview(progressView);
var delegate = DownloadSessionDelegate.sharedInstance;
delegate.storePath=sToLocation;
//here you pass progressView from ViewController to DownloadSessionDelegate
delegate.progressView = progressView
struct SessionProperties {
static let identifier : String! = "url_session_background_download"
}
var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
var url = NSURLRequest(URL: NSURL(string: sURL)!)
var downloadTask = backgroundSession.downloadTaskWithRequest(url)
downloadTask.resume()
}