如何在swift中从下载委托更新一个gui(progressview)

我有一个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() } 

要引用另一个类的进度视图,您需要将进度视图的实例传递给需要引用的类,在您的情况下:

 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() }