本文介绍了如何快速从下载委托更新 gui(进度视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 DownloadSessionDelegate 类来处理大文件的下载过程.我想在进度视图中显示进度.有关下载状态的信息在我的 DownloadSessionDelegate 类中.现在我不知道如何在该课程之外更新我的进度视图.
I have a DownloadSessionDelegate Class to handle a downloadprocess for big files.I would like to show the progress in a progressview. The Information about the download state is in my DownloadSessionDelegate Class. Now I don't know howto update my progressview outside of that 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 触发下载:
Triggering the download from my 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()
}
推荐答案
要从另一个类引用进度视图,您需要将进度视图的实例传递给需要其引用的类,在您的情况下:
To reference progress view from another class you will need to pass instance of progress view to the class that needs its reference, in your case:
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
}
}
视图控制器
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()
}
这篇关于如何快速从下载委托更新 gui(进度视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!