如何从 swift 中的下载委托更新 gui(进度视图)
Howto Update a gui (progressview) from a download delegate in swift
我有一个 DownloadSessionDelegate Class 来处理大文件的下载过程。
我想在进度视图中显示进度。有关下载状态的信息在我的 DownloadSessionDelegate Class 中。现在我不知道如何在 class 之外更新我的进度视图。
如何做到这一点?
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 引用进度视图,您需要将进度视图的实例传递给需要其引用的 class,在您的情况下:
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()
}
我有一个 DownloadSessionDelegate Class 来处理大文件的下载过程。 我想在进度视图中显示进度。有关下载状态的信息在我的 DownloadSessionDelegate Class 中。现在我不知道如何在 class 之外更新我的进度视图。
如何做到这一点?
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 引用进度视图,您需要将进度视图的实例传递给需要其引用的 class,在您的情况下:
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()
}